搜尋套件、儲存庫與 APP 分類

套件詳情v2.0.0+1

regexed_validator

驗證:電話、姓名、郵政編碼、電子郵件、網址、貨幣、IP、日期、時間、HTML 標籤、密碼(強)、中等密碼、信用卡。適用於 Dart/Flutter 開發者

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
47
喜歡數
55
Pub 分數
150

安裝套件

flutter pub add regexed_validator

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

regexed_validator 是一個 Dart/Flutter 套件,旨在透過為常見資料類型(如電子郵件、電話號碼、信用卡、密碼、URL、IP 位址、日期等)提供預建的正則表示式來簡化表單驗證。它為開發者提供可靠且可重複使用的驗證邏輯,確保在 Android、iOS、Web、macOS、Windows 和 Linux 上輸入格式的準確性。該套件支援強密碼與中等密碼強度檢查、HTML 標籤驗證以及貨幣格式化,非常適合建立穩健的使用者表單。透過清晰的語法和透過 pub.dev 的簡單整合,它提升開發效率,同時減少冗餘程式碼。

適用情境

  • 表單輸入驗證
  • 使用者註冊流程
  • 安全密碼檢查
  • 位址與聯絡表單處理
  • 行動與Web應用中的資料完整性
  • API 請求負載驗證

主要特色

  • 預定義的正則表達式模式,適用於12種以上的資料類型
  • 密碼強度驗證(強/中)
  • 跨平台相容性
  • 與 Flutter 表單輕鬆整合
  • 對國際格式的支援
  • 輕量且無依賴

適合對象

  • Flutter 開發人員建構表單
  • 需要嚴格輸入驗證的應用程式
  • 尋求可重複使用驗證邏輯的開發者
  • 針對多個平台的專案
  • 重視程式碼一致性的團隊
  • 需要快速表單開發的新創企業

使用建議

  • 需要手動處理錯誤訊息
  • 複雜驗證規則的自訂功能有限
  • 無內建的本地化支援
  • 依賴外部正則表達式模式
  • 不適用於高度動態的驗證情境
  • 可能需要額外的套件來實現進階表單管理

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Validating:

  • phone
  • name
  • postalCode
  • email
  • url
  • currency
  • ip
  • date
  • time
  • htmlTags
  • password (strong)
  • password (medium)
  • creditCard

For Dart/Flutter developers. Regexed Validator at pub.dartlang.org

Usage

Usage is pretty simple just pass a phone, date, email... to its validation method, see the example below

import 'package:regexed_validator/regexed_validator.dart';

main() {
  print( validator.email('example@example.co.uk') ); // true

  print(validator.url('http://www.nowhere.com?product=28&color=blue')); // true

  print(validator.currency('£498.10')); // true

  print(validator.ip('67.52.159.38')); // true

  print(validator.time('14:34 GMT -5')); // true

  print( validator.htmlTags('<strong>Bold</strong>\n'
          '<em>Emphazied</em>\n'
          '<b>Bold</b>\n'
          '<i>Italics</i>\n'
          '<span id="foo" class="bar">Some text</span>\n'
          '<hr />')
  ); // true
}

With Flutter TextField, TextFormField and Form(s)

There is nothing specific to this package when working with Forms and TextFields, see the example below

TextFormField(
    // The validator receives the text that the user has entered, and use regexed_validator to check if it is a valid string.
    validator: (value) {
      if (!validator.email(value)) {
        return 'Please enter a valid email';
      }
      return null;
    },
  ),

Here is a complete example, with a Form and an email field:

import 'package:flutter/material.dart';
import 'package:regexed_validator/regexed_validator.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    const appTitle = 'Form Validation Demo';

    return MaterialApp(
      title: appTitle,
      home: Scaffold(
        appBar: AppBar(
          title: const Text(appTitle),
        ),
        body: const MyCustomForm(),
      ),
    );
  }
}

// Create a Form widget.
class MyCustomForm extends StatefulWidget {
  const MyCustomForm({Key? key}) : super(key: key);

  @override
  MyCustomFormState createState() {
    return MyCustomFormState();
  }
}

// Create a corresponding State class.
// This class holds data related to the form.
class MyCustomFormState extends State<MyCustomForm> {
  // Create a global key that uniquely identifies the Form widget
  // and allows validation of the form.
  //
  // Note: This is a GlobalKey<FormState>,
  // not a GlobalKey<MyCustomFormState>.
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    // Build a Form widget using the _formKey created above.
    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          TextFormField(
            // The validator receives the text that the user has entered, and use regexed_validator to check if it is a valid string.
            validator: (value) {
              if (value == null || !validator.email(value)) {
                return 'Please enter a valid email';
              }
              return null;
            },
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 16.0),
            child: ElevatedButton(
              onPressed: () {
                // Validate returns true if the form is valid, or false otherwise.
                if (_formKey.currentState!.validate()) {
                  // If the form is valid, display a snackbar. In the real world,
                  // you'd often call a server or save the information in a database.
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: Text('Processing Data')),
                  );
                }
              },
              child: const Text('Submit'),
            ),
          ),
        ],
      ),
    );
  }
}

所屬分類

原始碼儲存庫

使用此套件的專案