Home > database >  Limit TextField can input letter and spacing only
Limit TextField can input letter and spacing only

Time:12-28

How can I limit the TextField can only input letter and spacing?

I had try:

TextField(
  inputFormatters: [
    RegexFormatter(regex: '[A-Za-z] '),
  ],
),

But it not work.

CodePudding user response:

Either of the 2 works.

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

or

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

CodePudding user response:

Try this,

TextField(
  inputFormatters: [
    FilteringTextInputFormatter.allow(RegExp("[a-zA-Z ]")),
    // This will allow only characters and space
  ],
), 

enter image description here

CodePudding user response:

Try this:

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

For further understanding please refer Restrict Special Character Input Flutter

CodePudding user response:

Try this Regexp

RegexFormatter(regex: '/^[a-zA-Z\s]*$/'),

CodePudding user response:

Refer below code

 TextField(
        inputFormatters: [
          FilteringTextInputFormatter.allow(
            RegExp("[a-zA-Z0-9 ]"),
          ),
          LengthLimitingTextInputFormatter(10),//for limit of max length
        ],
       ),
  • Related