删除 phone 输入 TextFormField 类型的第一个零 flutter
remove the first zeros of phone input TextFormField of type numbers flutter
如何删除 phone 数字的第一个零,例如 00963 和 031 等等 TextFormField
中的 flutter?
这是我的 TextFormField
代码:
TextFormField(
keyboardType: TextInputType.phone,
onSaved: (input) => _con.user.phone = input,
),
我的问题不是要阻止用户输入零,而是要用 phone 没有第一个零的数字来获取它,无论用户输入与否
String phone = '000345';
String editedPhone = phone.replaceFirst(RegExp(r'^0+'), "");
print(phone);
print(editedPhone);
将打印:
000345
345
如果您想从 phone 数字中删除所有第一个零,只需使用此正则表达式:
new RegExp(r'^0+')
^ - 匹配行首
0+ - 匹配零数字字符一次或多次
您的 TextFormField 的最终代码:
TextFormField(
keyboardType: TextInputType.phone,
onSaved: (input) => _con.user.phone = input.replaceFirst(new RegExp(r'^0+'), '');,
),
以上答案是正确的,但如果您使用 **TextFormField**
下面的示例将是值得的,
TextFormField(
controller: familyMemberPhoneController,
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp('[0-9]')),
//To remove first '0'
FilteringTextInputFormatter.deny(RegExp(r'^0+')),
//To remove first '94' or your country code
FilteringTextInputFormatter.deny(RegExp(r'^94+')),
],
...
如何删除 phone 数字的第一个零,例如 00963 和 031 等等 TextFormField
中的 flutter?
这是我的 TextFormField
代码:
TextFormField(
keyboardType: TextInputType.phone,
onSaved: (input) => _con.user.phone = input,
),
我的问题不是要阻止用户输入零,而是要用 phone 没有第一个零的数字来获取它,无论用户输入与否
String phone = '000345';
String editedPhone = phone.replaceFirst(RegExp(r'^0+'), "");
print(phone);
print(editedPhone);
将打印:
000345
345
如果您想从 phone 数字中删除所有第一个零,只需使用此正则表达式:
new RegExp(r'^0+')
^ - 匹配行首
0+ - 匹配零数字字符一次或多次
您的 TextFormField 的最终代码:
TextFormField(
keyboardType: TextInputType.phone,
onSaved: (input) => _con.user.phone = input.replaceFirst(new RegExp(r'^0+'), '');,
),
以上答案是正确的,但如果您使用 **TextFormField**
下面的示例将是值得的,
TextFormField(
controller: familyMemberPhoneController,
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp('[0-9]')),
//To remove first '0'
FilteringTextInputFormatter.deny(RegExp(r'^0+')),
//To remove first '94' or your country code
FilteringTextInputFormatter.deny(RegExp(r'^94+')),
],
...