搜索插件、代码仓与 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'),
            ),
          ),
        ],
      ),
    );
  }
}

所属分类

源码仓库

使用此插件的项目