skip to Main Content

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.

https://regexr.com/7fpho

3

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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.

    Login or Signup to reply.
  3. Try this

                                        TextField(
                                          keyboardType: const TextInputType
                                              .numberWithOptions(
                                            decimal: true,
                                          ),
                                          inputFormatters: [
                                            FilteringTextInputFormatter.allow(
                                              RegExp(r'^d*.?d{0,2}'),
                                            ),
                                          ],
                                        ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search