On a Flutter TextField
I have the following FilteringTextInputFormatter
:
FilteringTextInputFormatter.allow(RegExp(r"^d+.?d{1,2}"));
It tries to match a decimal number up to two places, but I can’t input any numbers into the TextField
.
On a Flutter TextField
I have the following FilteringTextInputFormatter
:
FilteringTextInputFormatter.allow(RegExp(r"^d+.?d{1,2}"));
It tries to match a decimal number up to two places, but I can’t input any numbers into the TextField
.
3
Answers
After messing with RegExr, I found that what I wanted was:
RegExp(r"^d{1,4}(.d{0,2})?")
This expression allows four digits with two decimal places.
Do not filter based on a regex that targets the entire string when finished. Remember that to type "hello world", at some point you must type "hello" and a single space, so your regex that demands no space at the end will fail in a filter.
Instead, use a regex match in the final validation. Not in the filter.
Try this