Flutter - Validate a phone number using Regex

后端 未结 2 795
旧时难觅i
旧时难觅i 2021-01-31 19:23

In my futter mobile app, I am trying to validate a phone number using regex. Below are the conditions.

  1. Phone numbers must contain 10 digits.
2条回答
  •  臣服心动
    2021-01-31 19:51

    Validation using Regex:

    String validateMobile(String value) {
    String pattern = r'(^(?:[+0]9)?[0-9]{10,12}$)';
    RegExp regExp = new RegExp(pattern);
    if (value.length == 0) {
          return 'Please enter mobile number';
    }
    else if (!regExp.hasMatch(value)) {
          return 'Please enter valid mobile number';
    }
    return null;
    }                                                                                                
    

提交回复
热议问题