我有一个文本字段,我想在其中限制用户的输入范围。我想要第一个数字是零,第二个数字是三个或四个。如何在文本字段中实现这一点?我找不到任何理由来设置这个。
我的代码
TextFormField(
textAlign: TextAlign.center,
enableSuggestions: false,
autocorrect: false,
maxLength: 12,
inputFormatters: [ FilteringTextInputFormatter.digitsOnly],
keyboardType: TextInputType.number)**补充:我添加了这条指令但不起作用,现在有什么问题?
FilteringTextInputFormatter.deny(RegExp(r'^(?:[0][3-4])?[0-9]{12}$')),发布于 2022-09-20 22:13:50
您可以使用掩码或更改onChanged值来完成此操作。
尝试:
onChanged: (value) => {
int n = int.parse(value)??0;
int oldN = int.parse(myController.text.substring(0,1))??0;
switch(oldN){
case 0:
if (n != 2 || n != 3){
myController.text = myController.text.substring(0,myCobtroller.lenght-1);
}
}
}在本例中,如果第一个数字为0,而下一个数字与2或3不同,则控制器不会更改。
..。
我在这里为您创建了一个完整的示例:https://gist.github.com/sergiotucano/7738517c264b672da7ea6a15e676d231
发布于 2022-09-22 12:58:07
--这将很好地工作:
TextFormField(
textAlign: TextAlign.center,
enableSuggestions: false,
autocorrect: false,
maxLength: 12,
inputFormatters: [ FilteringTextInputFormatter.allow(RegExp("^0[34][0-9]*$")),],
keyboardType: TextInputType.number)https://stackoverflow.com/questions/73791363
复制相似问题