Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TextFormField clears input text even if the Regex expression is satisfied

I’m using RegExp for positive decimal numbers

TextFormField(
  keyboardType: TextInputType.numberWithOptions(decimal: true),
  inputFormatters: [
    FilteringTextInputFormatter.allow(RegExp(r'^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$')),
  ],
)

But when I enter something like 1.x, the TextFormField deletes the entire input. What am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

the issue is that your regular expression don’t give you the ability to have a . without a digit after it. you can copy past x.y but you can’t type it one char at a time.
the solution would be relative to what you want and prioritize, but I think this regex might solve the issue :

r'^\s*(?=.*[1-9])\d?\.*(?:\d{1,2})?\s*$'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading