Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for ONLY positive and negative integer in input formatter for a TextFormField

Tags:

regex

flutter

I am trying to have a TextFormField which only accepts negative and positive number of 5 digits.

The following code but it does restrict digits only and up to 5, but it is not accepting a negative sign.

new TextFormField(
    maxLines: 1,
    keyboardType: TextInputType.number,
    enableSuggestions: false,
    inputFormatters: [
        LengthLimitingTextInputFormatter(5),
        FilteringTextInputFormatter.allow(RegExp(r'-?[0-9]')),
    ],
    onChanged: (val) {},
);

I have tried the following regex: "-?[0-9]" but it does not allow the negative sign.

like image 408
pcba-dev Avatar asked Dec 12 '25 11:12

pcba-dev


1 Answers

ChangeRegExp(r'-?[0-9]') to RegExp(r'^-?[0-9]*'). It's not letting you input a "-" because your expression is saying that you must have a number, but just solely "-" invalidates that so the formatter doesn't let you input the negative.

like image 200
raw-chicken Avatar answered Dec 15 '25 04:12

raw-chicken