13
votes

I have a TextField that will only take numbers as an input. The Keyboard that appears allows for the input of the the following characters: "-(hyphen) ,(comma) .(period) and space". How can I prevent the user from entering all except the period.

child: new TextField(
  controller: _controller,
  maxLengthEnforced: true,
  keyboardType: TextInputType.number,
  maxLength: 4, //9999 upper limit
  ), //TextField

I've tried to use RegExp to take the _controller text, remove the characters and place it back into to field without any luck.

...//Add Listener to _controller
_controller.addListener(restrictCharacters);
...

//Listener
void restrictCharacters(){
  RegExp regexp = new RegExp(
    r"^|\-|\,|\ ",
    caseSensitive: false,
    multiLine: false,);
  String text = _controller.text;
  String chng = text.replaceaLL(regExp,"");
  _controller.text = chng;
}

When applied the cursor moves to beginning and the field keeps the - (hyphen) for example.

9

9 Answers

33
votes

In your TextInputField() include this paramenter

inputFormatters: [new WhitelistingTextInputFormatter(RegExp("[0-9]")),],

If you want to allow only letters the the Regex would be

inputFormatters: [new WhitelistingTextInputFormatter(RegExp("[a-zA-Z]")),],

Update WhitelistingTextInputFormatter has been deprecated

use FilteringTextInputFormatter like this:

TextField(
 inputFormatters: <TextInputFormatter>[
      FilteringTextInputFormatter.allow(RegExp("[0-9a-zA-Z]")),
  ], // Only numbers can be entered
),
12
votes

WhitelistingTextInputFormatter is deprecated. You should use FilteringTextInputFormatter.allow:

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

or FilteringTextInputFormatter.deny instead of BlacklistingTextInputFormatter

9
votes

Add a BlacklistingTextInputFormatter to your TextField.

  inputFormatters: [
    new BlacklistingTextInputFormatter(new RegExp('[\\.]')),
  ],

removes just . If you wanted to disallow, say, . and , change it to

  inputFormatters: [
    new BlacklistingTextInputFormatter(new RegExp('[\\.|\\,]')),
  ],
7
votes

Add this to your TextField for letters and numbers

inputFormatters: [new  WhitelistingTextInputFormatter(RegExp("[a-zA-Z0-9]")),],
4
votes

If you want double to be added,you can also use the format like this:

inputFormatters: [new FilteringTextInputFormatter.allow(RegExp("[0-9.]")),],

4
votes

Dart 2.13+, Flutter 1.27+

 inputFormatters:[FilteringTextInputFormatter.allow(RegExp('[a-zA-Z0-9]'))]
3
votes

For those who need to work with money format in the text fields:

To use only: , (comma) and . (period)

and block the symbol: - (hyphen, minus or dash)

as well as the: (blank space)

In your TextField, just set the following code:

keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: [BlacklistingTextInputFormatter(new RegExp('[ -]'))],

The simbols hyphen and space will still appear in the keyboard, but will become blocked.

0
votes

if you want to include English and Devanagari character also in your name then follow this regex pattern I have created this pattern for English letter and Devanagari letters with allow space

 inputFormatters: [ WhitelistingTextInputFormatter(RegExp("[a-zA-Z \u0900-\u097F]")),
                    LengthLimitingTextInputFormatter(30),
                  ],
0
votes

You can deny special characters with escape sequences or get any escape sequence from google which you want

Example:

inputFormatters: [
                    FilteringTextInputFormatter.deny(
                      RegExp("[a-zA-Z0-9\u0020-\u007E-\u0024-\u00A9]"),
                    ),
                    
                  ],