Rajesh
Rajesh

Reputation: 71

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

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.

Upvotes: 0

Views: 1679

Answers (2)

Sheetal Savani
Sheetal Savani

Reputation: 1428

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,
    ],

Upvotes: 3

Matin Soleimani
Matin Soleimani

Reputation: 77

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

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

Upvotes: 1

Related Questions