Flutter - Validate a phone number using Regex Flutter - Validate a phone number using Regex dart dart

Flutter - Validate a phone number using Regex


You could make the first part optional matching either a + or 0 followed by a 9. Then match 10 digits:

^(?:[+0]9)?[0-9]{10}$
  • ^ Start of string
  • (?:[+0]9)? Optionally match a + or 0 followed by 9
  • [0-9]{10} Match 10 digits
  • $ End of string

Regex demo


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;}                                                                                                


I used the RegExp provided by @Dharmesh

This is how you can do it with null safety.

bool isPhoneNoValid(String? phoneNo) {  if (phoneNo == null) return false;  final regExp = RegExp(r'(^(?:[+0]9)?[0-9]{10,12}$)');  return regExp.hasMatch(phoneNo);}

Usage:

bool isValid = isPhoneNoValid('your_phone_no');