Home > Back-end >  How to accept only numbers in TextFormFields?
How to accept only numbers in TextFormFields?

Time:05-30

I want to accept only numbers in textformfields. I have designed textformfield for accepting phone number. I have change keyboard type.

keyboardType: TextInputType.phone,

But, It can also accept special symbols or can paste other inputs also. Keyboard screenshots

CodePudding user response:

Try below code, refer FilteringTextInputFormatter

import below library

import 'package:flutter/services.dart';

Your Widget:

    TextFormField(
    keyboardType: TextInputType.number,
    inputFormatters: [FilteringTextInputFormatter.digitsOnly],
    //you can used below formater also
    /*  inputFormatters: [
          FilteringTextInputFormatter.allow(
            RegExp("[0-9]"),
          ),
        ],*/
  ),

CodePudding user response:

In Your TextFormField add:

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

You can also modify RegExp for supporting any kind of input characters.

CodePudding user response:

Try used input formatter

import 'package:flutter/src/services/text_formatter.dart';

TextFormField(
      keyboardType: TextInputType.phone,
      inputFormatters:
      [
        FilteringTextInputFormatter.digitsOnly
      ]
    )

CodePudding user response:

You can use FilteringTextInputFormatter.

 TextField(
            keyboardType: TextInputType.number,
            inputFormatters: <TextInputFormatter>[
              FilteringTextInputFormatter.digitsOnly
            ], // Only numbers can be entered
          ),

CodePudding user response:

Simply, This could Work:

keyboardType: TextInputType.number,
  • Related