int X = 0123456789,
_areAlwdCharsTyped = false;
我想检查用户在 TextFormField
中输入的值是否包含变量 X
中的任何值。它也需要实时发生。
例如,如果我在 vV2g
中输入 TextFormField
,它应该显示 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;
}
我怎样才能使用正则表达式或以任何其他方式实现这一目标?谢谢!
答案 0 :(得分:0)
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
来显示实时错误!