EN VI

Flutter- How to accept only numbers in TextFormFields?

2022-05-30 17:28:45

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.

Solution:

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]"),
          ),
        ],*/
  ),
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login