Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for double numbers in dart

As from my previous question I'm trying to allow only numbers in double format into a TextField. I have looked through the whole wide web and didn't find a Regex for dart.

TextFormField(
        inputFormatters: [
          WhitelistingTextInputFormatter(RegExp(r'\d*\.?\d+')) //<-- useless attempt to allow double digits only
        ])
like image 831
delmin Avatar asked Apr 14 '20 18:04

delmin


1 Answers

Update:

The WhitelistingTextInputFormatter was deprecated after v1.20.0-1.0.pre. So use FilteringTextInputFormatter:

FilteringTextInputFormatter.allow(RegExp(r'(^\d*\.?\d*)'))

Before v1.20.0-1.0.pre:

You can try with:

WhitelistingTextInputFormatter(RegExp(r'(^\d*\.?\d*)'))

And the following is a stricter version of this regex. If the user press any invalid character other than the desired format the field will get cleared.

WhitelistingTextInputFormatter(RegExp(r'(^\d*\.?\d*)$'))
like image 78
Midhun MP Avatar answered Nov 02 '22 15:11

Midhun MP