删除数字颤动类型的手机输入 TextFormField 的第一个零

Moh*_*Ali 6 flutter textformfield

如何在 flutter inside 中删除电话号码的第一个零,例如 00963 和 031 等TextFormField
这是我的代码TextFormField

TextFormField(
             keyboardType: TextInputType.phone,
              onSaved: (input) => _con.user.phone = input,
             ),
Run Code Online (Sandbox Code Playgroud)

我的问题不是阻止用户输入零,而是获取没有第一个零的电话号码(无论用户是否输入)

gsm*_*gsm 7

上面的答案是正确的,但如果您使用**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+')),
                  ],
...
Run Code Online (Sandbox Code Playgroud)


sav*_*vke 3

如果您想删除电话号码中的所有第一个零,只需使用以下正则表达式:

 new RegExp(r'^0+')
Run Code Online (Sandbox Code Playgroud)

^ - 匹配行的开头

0+ - 匹配零数字字符一次或多次

TextFormField 的最终代码:

TextFormField(
  keyboardType: TextInputType.phone,
  onSaved: (input) => _con.user.phone = input.replaceFirst(new RegExp(r'^0+'), '');,
),
Run Code Online (Sandbox Code Playgroud)