Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict "0" in First Character in Flutter

How to restrict "0" should not be placed at the first character in the text field in Flutter? Below is the sample dart code.

TextField(
                                          inputFormatters: <TextInputFormatter>[
                                            FilteringTextInputFormatter.allow(
                                                RegExp("[0-9\u0660-\u0669]")),
                                          ],
                                          keyboardType: TextInputType.number,
                                          focusNode: _nodeText1,
                                          controller: mobileNumber,
                                          decoration: InputDecoration.collapsed(
                                              fillColor: TuxedoColor.whiteColor,
                                              hintText: "5xxxxxxxx"),
                                        ),
like image 282
Mohammed Nabil Avatar asked Oct 29 '25 21:10

Mohammed Nabil


2 Answers

Use this :

   inputFormatters: <TextInputFormatter>[
                                FilteringTextInputFormatter.allow(
                                  RegExp(r'[0-9]'),
                                ),
                                FilteringTextInputFormatter.deny(
                                  RegExp(
                                      r'^0+'), //users can't type 0 at 1st position
                                ),
                              ],
like image 88
Arijeet Avatar answered Oct 31 '25 11:10

Arijeet


Try below code use FilteringTextInputFormatter.deny

TextField(
      inputFormatters: <TextInputFormatter>[
        FilteringTextInputFormatter.allow(RegExp("[0-9\u0660-\u0669]")),
        FilteringTextInputFormatter.deny(RegExp(r'^0+')),
      ],
      keyboardType: TextInputType.number,
      decoration: InputDecoration.collapsed(hintText: "5xxxxxxxx"),
    ),
like image 20
Ravindra S. Patil Avatar answered Oct 31 '25 11:10

Ravindra S. Patil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!