Regex for double numbers in dart

1,714

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*)$'))
Share:
1,714
delmin
Author by

delmin

Updated on December 19, 2022

Comments

  • delmin
    delmin over 1 year

    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
            ])
    
  • Satria Suria
    Satria Suria over 2 years
    WhitelistingTextInputFormatter has been deprecated. Use FilteringTextInputFormatter instead.
  • Midhun MP
    Midhun MP over 2 years
    @SatriaSuria Thanks for the comment, I've updated the answer