如何限制顫動的 Textfield 或 TextFormField 只接受英語?不應允許其他語言進入。
uj5u.com熱心網友回復:
試試這個
TextField(
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp("^[\u0000-\u007F] \$"))
])
或者,如果你只想要英文字符,你可以試試這個。
TextField(
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp("[a-zA-Z]"))
])
uj5u.com熱心網友回復:
TextField(
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp('[a-z A-Z 0-9]'))
],
)
uj5u.com熱心網友回復:
您可以使用英語的正則運算式
bool validEnglish(String value) {
RegExp regex = RegExp(r'/^[A-Za-z0-9]*$');
return (!regex.hasMatch(value)) ? false : true;
}
TextFormField(
decoration: InputDecoration(
hintText: 'Enter text',
),
textAlign: TextAlign.center,
validator: (text) {
if (text == null || text.isEmpty || !validEnglish(text)) {
return 'Text is empty or invalid' ;
}
return null;
},
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/421009.html
標籤:
