'Flutter add dash after every 3 number textfield input and limit number of figure input

I'm looking for a way to add ( - ) after every number input in flutter textformfield and to limit the number of input to just 9 figure. For example 145-123-234. I will appreciate if anyone can help with this.



Solution 1:[1]

us input formatters:

inputFormatters: [ FilteringTextInputFormatter.allow(RegExp('[0-9,-]')), LengthLimitingTextInputFormatter(9), ],

this may help you number of input digits will work fine and regex expression for dash after 3 will change in your case in your case may change.

Solution 2:[2]

You can use mask text formatter package sample usage:

 var maskFormatter = new MaskTextInputFormatter(
      mask: '+# (###) ###-##-##', filter: {"#": RegExp(r'[0-9]')});
      
   TextField(
                      inputFormatters: [ maskFormatter],
                      autoFocus: false,
                      inputType: TextInputType.phone,
                      inputAction: TextInputAction.done,
                      },
                    ),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdul Rahman Panhyar
Solution 2 Yasin Ege