Regex for Mobile Number With or Without Country Code

Regex for Mobile Number with or without Country Code

From what I can see, this should work. The prefix is optional and is stored into the first match group, with the main number going into the second group.

^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$

But if you can give us some test cases for each, it'd help us in giving you a working regex for what you want.

Props to SchlaWiener in the comments for the correct limit on the country code length.

Regex for Mobile Number Validation

Satisfies all your requirements if you use the trick told below

Regex: /^(\+\d{1,3}[- ]?)?\d{10}$/

  1. ^ start of line
  2. A + followed by \d+ followed by a or - which are optional.
  3. Whole point two is optional.
  4. Negative lookahead to make sure 0s do not follow.
  5. Match \d+ 10 times.
  6. Line end.

DEMO Added multiline flag in demo to check for all cases

P.S. You really need to specify which language you use so as to use an if condition something like below:

// true if above regex is satisfied and (&&) it does not (`!`) match `0`s `5` or more times

if(number.match(/^(\+\d{1,3}[- ]?)?\d{10}$/) && ! (number.match(/0{5,}/)) )

RegEx for valid international mobile phone number

After stripping all characters except '+' and digits from your input, this should do it:

^\+[1-9]{1}[0-9]{3,14}$

If you want to be more exact with the country codes see this question on List of phone number country codes

However, I would try to be not too strict with my validation. Users get very frustrated if they are told their valid numbers are not acceptable.

Regular expression to match standard 10 digit phone number

^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$

Matches the following

123-456-7890
(123) 456-7890
123 456 7890
123.456.7890
+91 (123) 456-7890

If you do not want a match on non-US numbers use

^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$

Update :

As noticed by user Simon Weaver below, if you are also interested in matching on unformatted numbers just make the separator character class optional as [\s.-]?

^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$

Regex - Validate Mobile Phone with Optional Country Code

Make the groups optional by using ? which means "zero or one of the preceding":

/^([0-9]{3}-)?([0-9]{3})\-([0-9]{4})(\/[0-9]{4})?$/

? is a modifier like * (zero or more of the preceding) and + (one or more of the preceding).

This makes both groups optional which means that numbers with all three components (country code, number, extension), numbers with either of the optional components (number + extension, country code + number), and numbers with neither of the optional components, will be accepted by the regex.

EDIT

Your mistake in the regex in your comment is that the ? is after the escaped ), which means zero or one of an actual ) and not of a group. What you need is:

^(\(\+[0-9]{2}\))?([0-9]{3}-)?([0-9]{3})\-([0-9]{4})(\/[0-9]{4})?$

What regular expression will match valid international phone numbers?

\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$

Is the correct format for matching a generic international phone number. I replaced the US land line centric international access code 011 with the standard international access code identifier of '+', making it mandatory. I also changed the minimum for the national number to at least one digit.

Note that if you enter numbers in this format into your mobile phone address book, you may successfully call any number in your address book no matter where you travel. For land lines, replace the plus with the international access code for the country you are dialing from.

Note that this DOES NOT take into account national number plan rules - specifically, it allows zeros and ones in locations that national number plans may not allow and also allows number lengths greater than the national number plan for some countries (e.g., the US).

Regex for mobile number with optional country code

This regex will work

^(249)?1\d{8}$

Regex Demo

For second part

(?=^1)(^\d{1,8}$|^\d{10,}$)|(?=^2491)(^\d{1,11}$|^\d{13,}$)
(?!^(2491|1))^(\d{9}|\d{12})$

Java Code

Scanner x = new Scanner(System.in);
String pattern = "^(249)?1\\d{8}$";
Pattern r = Pattern.compile(pattern);
while (true) {
String line = x.nextLine();
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println("Found -> " + m.group());
} else {
System.out.println("Not Found");
}
}

Ideone Demo

Regular Expression (RegEx) For Phone Numbers With Country Code Check

I wouldn't burn my fingers on this.

Just by looking at a phone number you can not judge whether it is valid or not, and even if it 'looks' valid it might not be the phone number you're looking for (ie. someone else's phone number).

You're trying to solve the problem at the wrong level. The only way to validate a phone number is to actually CALL it ;)

A regex that suffices your criteria:

/^(1|20|21|..etc)[1-9][0-9]+$/

list of country codes (seperated by | ) followed by a digit that is not 0, followed by any digit any number of times.

You can can't do the 13-length check and the country check in one regex because the length of the country codes vary (to my knowledge at least).
You can do the 13-length check by a generic regex like:

/^.{,13}$/

Matching anything up to 13 characters long

PHP - RegEx Matching Phone Numbers with or without country code

Your regex is #(\+\d+|0)(\d{9})$#



Related Topics



Leave a reply



Submit