Home > Mobile >  Is there any way to not allow special characters in a textform field in flutter?
Is there any way to not allow special characters in a textform field in flutter?

Time:08-12

I want a text form field which will accept only alphabets not digits and special characters. If the user clicks on special character also that character on click in key board type should not work.

CodePudding user response:

You can add inputFormatters property to your TextField widget like this:

inputFormatters: <TextInputFormatter>[
  FilteringTextInputFormatter.allow(RegExp("[a-zA-Z]")),
], 

CodePudding user response:

Adding a inputFormatter will work.

  • 'a-z' - small alphabets
  • 'A-Z' - Capital alphabets
  • ' ' - Will allow space between

To allow space between

    inputFormatter: [
          FilteringTextInputFormatter.allow(RegExp("[a-zA-Z ]")),
     ],

No space between

    inputFormatter: [
          FilteringTextInputFormatter.allow(RegExp("[a-zA-Z]")),
     ],

Digits only

    inputFormatter: [
           FilteringTextInputFormatter.digitsOnly,
    ],
  • Related