Home > Software engineering >  make TextField accept only numbers and sign in flutter
make TextField accept only numbers and sign in flutter

Time:07-23

I want the field to allow the entry of numbers and the sign only but all I could do was make the field allow the entry of numbers only and I couldn't make it accept the sign

here is my code

TextField(
      inputFormatters: [
        FilteringTextInputFormatter.allow(
          RegExp(
            "[0-9]",
          ),
        ),
      ],
    ),

CodePudding user response:

TextField(
        inputFormatters: [
          FilteringTextInputFormatter.allow(
          RegExp(
            r'^\ ?\d*',
          ),
        ),],
      )

This will accept an optional sign at the begining and the rest are numbers. The same as international phone numbers.

CodePudding user response:

  TextField(
            
         keyboardType:TextInputType.numberWithOptions(decimal:true,signed:true),
                        inputFormatters: [
                          
                        
           FilteringTextInputFormatter.allow(RegExp('[0-9.,] ')),
                        ],
        )

,

just add this.

  • Related