Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to accept only numbers in TextFormFields?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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]"),
          ),
        ],*/
  ),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading