PeakGen
PeakGen

Reputation: 23015

Flutter - Validate a phone number using Regex

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

  1. Phone numbers must contain 10 digits.
  2. In case country code us used, it can be 12 digits. (example country codes: +12, 012)
  3. No space or no characters allowed between digits

In simple terms, here is are the only "valid" phone numbers

0776233475, +94776233475, 094776233475

Below is what I tried, but it do not work.

String _phoneNumberValidator(String value) {
    Pattern pattern =
        r'/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/';
    RegExp regex = new RegExp(pattern);
    if (!regex.hasMatch(value))
      return 'Enter Valid Phone Number';
    else
      return null;
  }

How can I solve this?

Upvotes: 47

Views: 90324

Answers (6)

Mohand Adel
Mohand Adel

Reputation: 1

This answer contains verified phone numbers for the Republic of Egypt

 static bool isPhoneNumberValid(String phoneNumber) {
    return RegExp(r'^(010|011|012|015)[0-9]{8}$').hasMatch(phoneNumber);
  }

Upvotes: 0

Dharmesh Mansata
Dharmesh Mansata

Reputation: 4718

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

Upvotes: 57

String phoneNumberValidator(String value) {
    Pattern pattern =
        r'\+994\s+\([0-9]{2}\)\s+[0-9]{3}\s+[0-9]{2}\s+[0-9]{2}';
    RegExp regex = new RegExp(pattern);
    if (!regex.hasMatch(value))
      return 'Enter Valid Phone Number';
    else
      return null;
  }

Upvotes: 0

CopsOnRoad
CopsOnRoad

Reputation: 267464

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');

Upvotes: 3

Rahamat
Rahamat

Reputation: 49

  @override
  String validator(String value) {
    if (value.isEmpty) {
      return 'Mobile can\'t be empty';
    } else if (value.isNotEmpty) {
      //bool mobileValid = RegExp(r"^(?:\+88||01)?(?:\d{10}|\d{13})$").hasMatch(value);

      bool mobileValid =
          RegExp(r'^(?:\+?88|0088)?01[13-9]\d{8}$').hasMatch(value);
      return mobileValid ? null : "Invalid mobile";
    }
  }

Upvotes: 5

The fourth bird
The fourth bird

Reputation: 163217

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

Upvotes: 67

Related Questions