int X = 0123456789,
_areAlwdCharsTyped = false;我想检查用户在TextFormField中输入的值是否包含变量X中的任何值。它也需要实时发生。
例如,如果我在TextFormField中输入vV2g,它应该显示X包含textEditingController.text的值。
我尝试使用正则表达式来实现这一点。它工作得很好,除了当我输入一个数字,删除它,然后只输入一些字母时,_areAlwdCharsTyped仍然返回false。
int X = 0123456789,
if (myPsWrdController.text.isNotEmpty)
{
String allowedChar =
X;
final split = textEditingController.text.split('');
split.forEach((c) {
if (textEditingController.text.isNotEmpty &&
allowedChar.contains(c)) {
_areAlwdCharsTyped = true;
} else if (textEditingController.text.isEmpty &&
!allowedChar.contains(c)) {
_areAlwdCharsTyped = false;
} else {
_areAlwdCharsTyped = false;
}
});
} else {
_areAlwdCharsTyped = false;
}我如何使用正则表达式或任何其他方式来实现这一点?谢谢!
发布于 2021-04-28 13:02:58
RegExp reg = RegExp(r'^[cAzdTnJ574]*$');
//change the characters inside [] to your own !
String error = '';
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.white,
body: Center(
child: StatefulBuilder(
builder: (context, state) {
return TextField(
decoration: InputDecoration(
errorText: error,
),
style: TextStyle(color: Colors.black,),
onChanged:(str){
if(str.isEmpty){
state((){
error = '';
});
}
else if(!reg.hasMatch(str)){
state((){
error = 'Input invalid !';
});
}else{
state((){
error = '';
});
}
}
);
}
),
),
),
);
}我使用StatefulBuilder来显示实时错误!
https://stackoverflow.com/questions/67280606
复制相似问题