英文:
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<FormState>();
MaskTextInputFormatter _birthdayFormatter = MaskTextInputFormatter(
mask: '##/##/####',
filter: {"#": RegExp(r'[0-9]')},
);
**Form**
Form(
key: _formKey,
child: TextFormField(
inputFormatters: [_birthdayFormatter],
decoration: InputDecoration(
hintText: 'dd/mm/yyyy',
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter date';
}
List<String> dateParts = value.split('/');
if (dateParts.length != 3) {
return 'Date must be in dd/mm/yyyy format';
}
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 'Day must be a number between 1 and 31';
}
if (month == null || month < 1 || month > 12) {
return 'Month must be a number between 1 and 12';
}
if (year == null || year < 1900 || year > 3000) {
return 'Year must be a number between 1900 and 3000';
}
// Create a DateTime to check if the date is correct.
DateTime? date =
DateTime.tryParse('$year-$month-$day');
if (date == null) {
return 'Invalid date';
}
return null;
},
),
),
**Validator**
if (_formKey.currentState!.validate()) {
// If the form is valid, display a Snackbar.
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Processing Data')));
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论