无夜之星辰
无夜之星辰

Reputation: 6148

Limit TextField can input letter and spacing only

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.

Upvotes: 1

Views: 110

Answers (5)

Ravindra S. Patil
Ravindra S. Patil

Reputation: 14775

Refer below code

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

Upvotes: 1

ShinMyth
ShinMyth

Reputation: 644

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

Upvotes: 2

Nams
Nams

Reputation: 302

Try this Regexp

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

Upvotes: 1

krishnaacharyaa
krishnaacharyaa

Reputation: 24912

Try this:

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

For further understanding please refer Restrict Special Character Input Flutter

Upvotes: 1

Anandh Krishnan
Anandh Krishnan

Reputation: 5986

Try this,

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

enter image description here

Upvotes: 2

Related Questions