1

Hello I am creating a mobile number verification screen. In this I want to validate the Indian mobile number. But my code is not validating and I am not able to understand where the problem is. If the text form field is validated, then the api is called and if the text form field is not validated then the text form field is highlighted with alert. Please help me in resolving this issue.

Form(
        key: _formKeymobile,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              S.of(context).verify,
              style: TextStyle(
                  fontSize: 30.0.sp,
                  fontFamily: 'DancingScript',
                  color: Colors.purple),
            ),
            SizedBox(
              height: 20,
            ),
            TextFormField(
              style: TextStyle(color: Colors.blue, fontSize: 20.0.sp),
              controller: _mobilenumber,
              validator: (value) {
                const pattern =
                    r'(^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[6789]\d{9}|(\d[ -]?){10}\d$)';
                final regExp = RegExp(pattern);
                if (value!.isEmpty) {
                  return S.of(context).EnterSomeText;
                } else if (value.length != 10) {
                } else if (!regExp.hasMatch(value)) {
                  return S.of(context).Mobilenumbermust10digits;
                  return S.of(context).MobileNumbermustbedigits;
                }
              },
              maxLength: 11,
              keyboardType: TextInputType.phone,
              decoration: InputDecoration(
                  hintText: S.of(context).mobile,
                  border: OutlineInputBorder()),
            ),
            SizedBox(
              height: 8.0,
            ),
            Row(
              children: [
                Checkbox(
                  checkColor: Colors.white,
                  value: isChecked,
                  onChanged: (bool? value) {
                    setState(() {
                      isChecked = value!;
                    });
                    // print(isChecked);
                  },
                ),
                SizedBox(
                  width: MediaQuery.of(context).size.width * 0.60,
                  child: Column(
                    children: [
                      Text(S.of(context).tcone),
                      InkWell(
                          onTap: () {
                            openWebsite(
                                
                                context);
                          },
                          child: Text(
                            S.of(context).tctwo,
                            style: TextStyle(color: Colors.blue),
                          )),
                      Text("and"),
                      InkWell(
                          onTap: () {
                            openWebsite(
                                
                                context);
                          },
                          child: Text(
                            S.of(context).tcthree,
                            style: TextStyle(color: Colors.blue),
                          )),
                    ],
                  ),
                )
              ],
            ),
            ElevatedButton(
              onPressed: () async {
                if (isChecked == true) {
                  if (_formKeymobile.currentState!.validate() &&
                      isChecked == true) {
                    String? deviceId = await _getId();
                    if (deviceId != null) {
                      print("api called");
                    } else {
                      EasyLoading.showError("Unable to get device id");
                    }
                  }
                } else {
                  EasyLoading.showError("Please click the box");
                }
              },
              style: ButtonStyle(
                  backgroundColor:
                      MaterialStateProperty.all(Colors.purple)),
              child: Text(S.of(context).otp),
            ),
            SizedBox(
              height: 8.0,
            ),
            ElevatedButton(
              onPressed: () async {
                if (_formKeymobile.currentState!.validate() &&
                    isChecked == true) {
                  print("form validated");
                }
              },
              style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.green)),
              child: Text("Already have OTP"),
            ),
          ],
        )

1 Answer 1

1

try this:

validator: (value) {
                const pattern =
                    r'(^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[6789]\d{9}|(\d[ -]?){10}\d$)';
                final regExp = RegExp(pattern);
                if (value == null || value!.isEmpty) {
                  return S.of(context).EnterSomeText;
                } else if (value.length != 10) {
                } else if (!regExp.hasMatch(value)) {
                  return S.of(context).Mobilenumbermust10digits;
                  return S.of(context).MobileNumbermustbedigits;
                }
              },

and also change this:

onPressed: () async {
                if (_formKeymobile.currentState!.validate() &&
                      isChecked == true) {
                    String? deviceId = await _getId();
                    if (deviceId != null) {
                      print("api called");
                    } else {
                      EasyLoading.showError("Unable to get device id");
                    }
                  } else {
                  EasyLoading.showError("Please click the box");
                }
              },

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

glad to hear that, If it was useful to you please approve and up vote it so other people find it easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.