Regex for double numbers in dart

Issue

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
        ])

Solution

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*)$'))

Answered By – Midhun MP

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published