Search code examples
fluttertextformfield

remove the first zeros of phone input TextFormField of type numbers flutter


how can i remove the first zeros of phone number like 00963 and 031 and so on in flutter inside TextFormField ?
here is my code of theTextFormField :

TextFormField(
             keyboardType: TextInputType.phone,
              onSaved: (input) => _con.user.phone = input,
             ),

my question is not to prevent the user enter the zeros but to get it with phone number without first zeros if the user entered it or not


Solution

  • If you want to remove all first zeros from phone number just use this regex expression:

     new RegExp(r'^0+')
    

    ^ - match the beginning of a line

    0+ - match the zero digit character one or more times

    Final code for your TextFormField:

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