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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With