正则表达式掩码,使用’mask_text_input_formatter’验证两位数字。

huangapple go评论67阅读模式
英文:

Regex mask with two digit number validation using 'mask_text_input_formatter'

问题

我想为输入掩码中的月份部分创建一个正则表达式。到目前为止,这是我所拥有的。但是,使用这种方法,我无法在月份部分写入任何内容,因为它只检查一个数字('m'),而我认为它期望有两个数字。

MaskTextInputFormatter _birthdayFormatter = MaskTextInputFormatter(
  mask: 'dd/mm/yyyy',
  filter: {'d': RegExp(r'[0-9]'), 'm': RegExp(r'(^0?[1-9]$)|(^1[0-2]$)'), 'y': RegExp(r'[0-9]')}
);

有没有一种方法可以实现这个?

英文:

I want to create a regexp for the month part in my input mask. This is what I have so far. But with this approach I can't write anything in the month part because it checks for only one digit ('m') and it expects to be two digits I think.

  MaskTextInputFormatter _birthdayFormatter = MaskTextInputFormatter(
    mask: 'dd/mm/yyyy',
    filter: {'d': RegExp(r'[0-9]'), 'm': RegExp(r'(^0?[1-9]$)|(^1[0-2]$)'), 'y': RegExp(r'[0-9]')}
  );

Is there a way to achieve this?

答案1

得分: 1

**掩码和密钥**

```dart
final _formKey = GlobalKey<FormState>();

MaskTextInputFormatter _birthdayFormatter = MaskTextInputFormatter(
  mask: '##/##/####',
  filter: {"#": RegExp(r'[0-9]')},
);

表单

Form(
  key: _formKey,
  child: TextFormField(
    inputFormatters: [_birthdayFormatter],
    decoration: InputDecoration(
      hintText: 'dd/mm/yyyy',
    ),
    validator: (value) {
      if (value == null || value.isEmpty) {
        return '请输入日期';
      }
      List<String> dateParts = value.split('/');
      if (dateParts.length != 3) {
        return '日期必须采用dd/mm/yyyy格式';
      }

      int? day = int.tryParse(dateParts[0]);
      int? month = int.tryParse(dateParts[1]);
      int? year = int.tryParse(dateParts[2]);

      if (day == null || day < 1 || day > 31) {
        return '日期必须在1到31之间';
      }

      if (month == null || month < 1 || month > 12) {
        return '月份必须在1到12之间';
      }

      if (year == null || year < 1900 || year > 3000) {
        return '年份必须在1900到3000之间';
      }

      // 创建一个DateTime对象以检查日期是否正确。
      DateTime? date = DateTime.tryParse('$year-$month-$day');
      if (date == null) {
        return '无效日期';
      }

      return null;
    },
  ),
)

验证器

if (_formKey.currentState!.validate()) {
  // 如果表单有效,请显示Snackbar。
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(content: Text('处理数据')));
}

<details>
<summary>英文:</summary>

**Mask and key**

     final _formKey = GlobalKey&lt;FormState&gt;();
    
        MaskTextInputFormatter _birthdayFormatter = MaskTextInputFormatter(
          mask: &#39;##/##/####&#39;,
          filter: {&quot;#&quot;: RegExp(r&#39;[0-9]&#39;)},
        );

**Form**

    Form(
                            key: _formKey,
                            child: TextFormField(
                              inputFormatters: [_birthdayFormatter],
                              decoration: InputDecoration(
                                hintText: &#39;dd/mm/yyyy&#39;,
                              ),
                              validator: (value) {
                                if (value == null || value.isEmpty) {
                                  return &#39;Please enter date&#39;;
                                }
                                List&lt;String&gt; dateParts = value.split(&#39;/&#39;);
                                if (dateParts.length != 3) {
                                  return &#39;Date must be in dd/mm/yyyy format&#39;;
                                }
    
                                int? day = int.tryParse(dateParts[0]);
                                int? month = int.tryParse(dateParts[1]);
                                int? year = int.tryParse(dateParts[2]);
    
                                if (day == null || day &lt; 1 || day &gt; 31) {
                                  return &#39;Day must be a number between 1 and 31&#39;;
                                }
    
                                if (month == null || month &lt; 1 || month &gt; 12) {
                                  return &#39;Month must be a number between 1 and 12&#39;;
                                }
    
                                if (year == null || year &lt; 1900 || year &gt; 3000) {
                                  return &#39;Year must be a number between 1900 and 3000&#39;;
                                }
    
                                // Create a DateTime to check if the date is correct.
                                DateTime? date =
                                    DateTime.tryParse(&#39;$year-$month-$day&#39;);
                                if (date == null) {
                                  return &#39;Invalid date&#39;;
                                }
    
                                return null;
                              },
                            ),
                          ),
    
**Validator**

    if (_formKey.currentState!.validate()) {
                                        // If the form is valid, display a Snackbar.
                                        ScaffoldMessenger.of(context).showSnackBar(
                                            SnackBar(
                                                content: Text(&#39;Processing Data&#39;)));
                                      }

</details>



huangapple
  • 本文由 发表于 2023年7月11日 03:18:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定