skip to Main Content

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.

5

Answers


  1. Try this,

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

    enter image description here

    Login or Signup to reply.
  2. Try this:

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

    For further understanding please refer
    Restrict Special Character Input Flutter

    Login or Signup to reply.
  3. Refer below code

     TextField(
            inputFormatters: [
              FilteringTextInputFormatter.allow(
                RegExp("[a-zA-Z0-9 ]"),
              ),
              LengthLimitingTextInputFormatter(10),//for limit of max length
            ],
           ),
    
    Login or Signup to reply.
  4. Try this Regexp

    RegexFormatter(regex: '/^[a-zA-Zs]*$/'),
    
    Login or Signup to reply.
  5. Either of the 2 works.

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

    or

    inputFormatters: <TextInputFormatter>[
       FilteringTextInputFormatter.allow(
          RegExp(r"[a-zA-Z ]"),
       )
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search