Flutter 如何只允许逗号(.)后有2位和1位数字?
我有一个问题,我不能得到正确的正则表达式在一起。 我的目标是允许逗号前最多有3位数字,并且只有在有小数点时,逗号后才有1位数字。对于这种行为,我必须使用哪些RegExp或regex 想要的允许结果:000.0,00.0,0.0,000,00,0 这是当前的代码,但问题是这里也可以放置4位数字,不带小数:Flutter 如何只允许逗号(.)后有2位和1位数字?,flutter,dart,Flutter,Dart,我有一个问题,我不能得到正确的正则表达式在一起。 我的目标是允许逗号前最多有3位数字,并且只有在有小数点时,逗号后才有1位数字。对于这种行为,我必须使用哪些RegExp或regex 想要的允许结果:000.0,00.0,0.0,000,00,0 这是当前的代码,但问题是这里也可以放置4位数字,不带小数: inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'^\d{1,3}\.?\d{0,1}')), ], 我已经浏览了
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^\d{1,3}\.?\d{0,1}')),
],
我已经浏览了这些,但它们不适合我:
试试这个正则表达式
我还认为逗号是小数点的意思。考虑到小数点前需要3位,小数点后需要1位
TextFormField(
autoValidateMode: AutoValidateMode.always,
validator: (value) {
return RegExp(r'^\d{0,3}(\.\d{1})?$').hasMatch(value) ? null : 'Invalid value';
},
)
试试这个正则表达式
我还认为逗号是小数点的意思。考虑到小数点前需要3位,小数点后需要1位
TextFormField(
autoValidateMode: AutoValidateMode.always,
validator: (value) {
return RegExp(r'^\d{0,3}(\.\d{1})?$').hasMatch(value) ? null : 'Invalid value';
},
)
我不是正则表达式专家,因此我只能建议您使用以下帮助函数:
bool match(String input) {
if (input.split('.').length == 1) {
// if there is no dot in the string
// returns true if the length is < 4
return (input.length < 4) ? true : false;
} else if (input.split('.').length > 1){
// if there is more than one dot in the string
return false;
} else {
// if there is a dot in the string
// returns true if there are < 4 digits before the dot and exactly 1 digit
// after the dot
return (input.split('.')[0].length < 4 && input.split('.')[1].length == 1)
? true
: false;
}
}
我不是正则表达式专家,因此我只能建议您使用以下帮助函数:
bool match(String input) {
if (input.split('.').length == 1) {
// if there is no dot in the string
// returns true if the length is < 4
return (input.length < 4) ? true : false;
} else if (input.split('.').length > 1){
// if there is more than one dot in the string
return false;
} else {
// if there is a dot in the string
// returns true if there are < 4 digits before the dot and exactly 1 digit
// after the dot
return (input.split('.')[0].length < 4 && input.split('.')[1].length == 1)
? true
: false;
}
}
这里的输入格式化程序不是这种情况,因为它可以直观地格式化数据,并且所需的格式相互包含。您应该将TextFormField和validator属性与@Andrej的validator一起使用,或者使用RegExp TextFormField autoValidateMode:autoValidateMode.always, 验证器:值{ 返回RegExr'^\d{1,3}\.\d?$'.hasMatchvalue?null:'无效值'; },
RegExp正在工作。这里的输入格式化程序不是这种情况,因为它可以直观地格式化数据,并且所需的格式相互包含。您应该将TextFormField和validator属性与@Andrej的validator一起使用,或者使用RegExp TextFormField autoValidateMode:autoValidateMode.always, 验证器:值{ 返回RegExr'^\d{1,3}\.\d?$'.hasMatchvalue?null:'无效值'; },
RegExp正在工作。不幸的是,dart中不起作用,是的,我指的是十进制,我编辑了我的问题是,您必须使用验证器,让我编辑answer@Nuqo我添加了一个示例,说明如何在dart中使用正则表达式。如果它对您有效,您也可以将其标记为已接受。不幸的是,它在dart中不起作用,是的,我是说小数点,我编辑了我的问题是的,你必须使用验证器,让我来编辑一下answer@Nuqo我已经添加了一个示例,说明如何在dart中使用正则表达式。如果它对您有效,您也可以将其标记为已接受。如何在扩展“TextInputFormatter”的自定义类中使用它,或将其用作inputFormatter?就像BambinoUA在其答案中使用的那样,使用验证器。PS我对代码进行了一些编辑。谢谢,我最终使用了你的解决方案,它与我的第一个“FilteringTemperator”一起被剽窃。没问题,我很高兴我帮助了你。如果你对此有任何疑问,请询问我。如何在扩展“TextInputFormatter”的自定义类中使用此选项,或将其用作inputFormatter?就像BambinoUA在回答中使用验证器那样。PS我对代码进行了一些编辑。谢谢,我最终使用了你的解决方案,它与我的第一个“FilteringTemperator”一起被剽窃。没问题,我很高兴我帮助了你。如果你对此还有任何问题,请问我。