flutter 输入框限制(手机号,邮箱,身份证、银行卡)

通过TextField组件 输入框inputFormatters属性限制

FilteringTextInputFormatter.allow() (白名单校验),表示只允许输入符合规则的字符 ;
FilteringTextInputFormatter.deny()(黑名单校验),除了规定的字符,其他都可以输入;
LengthLimitingTextInputFormatter (),(长度限制)

常用正则匹配

  /// 禁止输入空格
  static const String regexNotNull = "[\\s]";
  /// 第一个输入字符不能为空格
  static const String regexFirstNotNull = r'^(\S){1}';
  ///仅支持数字
  static const String regexOnlyNumber = "[0-9]";
  ///仅支持字母和数字
  static const String regexOnlyNumberText = "[a-zA-Z]|[0-9]";
  1. 邮箱: (邮箱键盘,禁止输入空格,限制30位)
   TextField(
        key: emailKey,
        keyboardType: TextInputType.emailAddress,
        controller: logic.emailController,
        inputFormatters: [
          LengthLimitingTextInputFormatter(30),
          FilteringTextInputFormatter.deny(
            RegExp(RegexUtil.regexNotNull),
          )
        ],
        onChanged: (value) {
        
        },
      ),
  1. 手机号:(手机号键盘,仅支持数字,禁止输入空格)
              TextField(
                      keyboardType: TextInputType.phone,
                      inputFormatters: [
                        FilteringTextInputFormatter.allow(
                          RegExp(RegexUtil.regexOnlyNumber),
                        ),
                        FilteringTextInputFormatter.deny(
                          RegExp(RegexUtil.regexNotNull),
                        )
                      ],
                      // suffixIcon: assetsImage('icon_contact.webp',
                      //     width: 17.w, height: 17.h),
                      onChanged: (value) {
                        logic.basicContactModel?.fancyGentleRice = value;
                        logic.update();
                      },
                    ),
  1. 银行卡:(限制30位,仅支持数字和字母,小写自动转大写)
        TextField(
              keyboardType: TextInputType.text,
              controller: logic.pwdController,
              inputFormatters: [
                LengthLimitingTextInputFormatter(30),
                FilteringTextInputFormatter.allow(
                  RegExp(RegexUtil.regexOnlyNumberText),
                )
              ],
              onChanged: (value) {
                ///自动转大写
                logic.pwdController.value = logic.pwdController.value
                    .copyWith(text: value.toUpperCase());
                logic.update();
              },
            )
  1. 首字母不能为空示例2
TextField(
          onChanged: (value) {
           String str = value.trimLeft();
            if (value.isNotEmpty && value[0] == ' ') {
              // 如果第一个字符是空格
              controller?.text = str;
              controller?.value = TextEditingValue(
                  text: str,
                  selection: TextSelection.fromPosition(TextPosition(
                      affinity: TextAffinity.downstream, offset: str.length)));
            }
            widget.onChanged?.call(str);
       )
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值