Phone validation regex

RegexPhone Number

Regex Problem Overview


I'm using this pattern to check the validation of a phone number

^[0-9\-\+]{9,15}$

It's works for 0771234567 and +0771234567, but I want it to works for 077-1234567 and +077-1234567 and +077-1-23-45-67 and +077-123-45-6-7

What should I change in the pattern?

Regex Solutions


Solution 1 - Regex

Please refer to this SO Post

https://stackoverflow.com/questions/6960596/example-of-a-regular-expression-in-jquery-for-phone-numbers

/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/
  • (123) 456 7899
  • (123).456.7899
  • (123)-456-7899
  • 123-456-7899
  • 123 456 7899
  • 1234567899

are supported

Solution 2 - Regex

enter image description hereThis solution actually validates the numbers and the format. For example: 123-456-7890 is a valid format but is NOT a valid US number and this answer bears that out where others here do not.


If you do not want the extension capability remove the following including the parenthesis: (?:\s*(?:#|x.?|ext.?|extension)\s*(\d+)\s*)? :)

edit (addendum) I needed this in a client side only application so I converted it. Here it is for the javascript folks:

var myPhoneRegex = /(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]‌​)\s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)([2-9]1[02-9]‌​|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})\s*(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+)\s*)?$/i;
if (myPhoneRegex.test(phoneVar)) {
    // Successful match
} else {
    // Match attempt failed
}

hth. end edit

This allows extensions or not and works with .NET

(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]‌​)\s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)([2-9]1[02-9]‌​|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$

To validate with or without trailing spaces. Perhaps when using .NET validators and trimming server side use this slightly different regex:

(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]‌​)\s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)([2-9]1[02-9]‌​|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})\s*(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+)\s*)?$

All valid:

1 800 5551212

800 555 1212

8005551212

18005551212

+1800 555 1212 extension65432

800 5551212 ext3333

Invalid #s

234-911-5678

314-159-2653

123-234-5678


> EDIT: Based on Felipe's comment I have updated this for international.

Based on what I could find out from here and here regarding valid global numbers

This is tested as a first line of defense of course. An overarching element of the international number is that it is no longer than 15 characters. I did not write a replace for all the non digits and sum the result. It should be done for completeness. Also, you may notice that I have not combined the North America regex with this one. The reason is that this international regex will match North American numbers, however, it will also accept known invalid # such as +1 234-911-5678. For more accurate results you should separate them as well.

Pauses and other dialing instruments are not mentioned and therefore invalid per E.164

\(?\+[0-9]{1,3}\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{4}( ?-?[0-9]{3})?

With 1-10 letter word for extension and 1-6 digit extension:

\(?\+[0-9]{1,3}\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{4}( ?-?[0-9]{3})? ?(\w{1,10}\s?\d{1,6})?

Valid International: Country name for ref its not a match.

+55 11 99999-5555 Brazil

+593 7 282-3889 Ecuador

(+44) 0848 9123 456 UK

+1 284 852 5500 BVI

+1 345 9490088 Grand Cayman

+32 2 702-9200 Belgium

+65 6511 9266 Asia Pacific

+86 21 2230 1000 Shanghai

+9124 4723300 India

+821012345678 South Korea

And for your extension pleasure

+55 11 99999-5555 ramal 123 Brazil

+55 11 99999-5555 foo786544 Brazil

Enjoy

Solution 3 - Regex

Consider:

^\+?[0-9]{3}-?[0-9]{6,12}$

This only allows + at the beginning; it requires 3 digits, followed by an optional dash, followed by 6-12 more digits.

Note that the original regex allows 'phone numbers' such as 70+12---12+92, which is a bit more liberal than you probably had in mind.


The question was amended to add:

> +077-1-23-45-67 and +077-123-45-6-7

You now probably need to be using a regex system that supports alternatives:

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

The first alternative is seven digits; the second is 1-23-45-67; the third is 123-45-6-7. These all share the optional plus + followed by 3 digits and an optional dash - prefix.

The comment below mentions another pattern:

> +077-12-34-567

It is not at all clear what the general pattern should be - maybe one or more digits separated by dashes; digits at front and back?

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

This will allow the '+077-' prefix, followed by any sequence of digits alternating with dashes, with at least one digit between each dash and no dash at the end.

Solution 4 - Regex

I have a more generic regex to allow the user to enter only numbers, +, -, whitespace and (). It respects the parenthesis balance and there is always a number after a symbol.

^([+]?[\s0-9]+)?(\d{3}|[(]?[0-9]+[)])?([-]?[\s]?[0-9])+$

false, ""
false, "+48 504 203 260@@"
false, "+48.504.203.260"
false, "+55(123) 456-78-90-"
false, "+55(123) - 456-78-90"
false, "504.203.260"
false, " "
false, "-"
false, "()"
false, "() + ()"
false, "(21 7777"
false, "+48 (21)"
false, "+"
true , " 1"
true , "1"
true, "555-5555-555"
true, "+48 504 203 260"
true, "+48 (12) 504 203 260"
true, "+48 (12) 504-203-260"
true, "+48(12)504203260"
true, "+4812504203260"
true, "4812504203260

Solution 5 - Regex

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

so first is a digit or a +, then some digits or -

Solution 6 - Regex

First test the length of the string to see if it is between 9 and 15.

Then use this regex to validate:

^\+?\d+(-\d+)*$

This is yet another variation of the normal* (special normal*)* pattern, with normal being \d and special being -.

Solution 7 - Regex

I tried :

^(1[ \-\+]{0,3}|\+1[ -\+]{0,3}|\+1|\+)?((\(\+?1-[2-9][0-9]{1,2}\))|(\(\+?[2-8][0-9][0-9]\))|(\(\+?[1-9][0-9]\))|(\(\+?[17]\))|(\([2-9][2-9]\))|([ \-\.]{0,3}[0-9]{2,4}))?([ \-\.][0-9])?([ \-\.]{0,3}[0-9]{2,4}){2,3}$

I took care of special country codes like 1-97... as well. Here are the numbers I tested against (from Puneet Lamba and MCattle):

***** PASS *****
18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1.800.555.1234
+1.800.555.1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
(+230) 5 911 4450
123345678
(1) 345 654 67
+1 245436
1-976 33567
(1-734) 5465654
+(230) 2 345 6568
***** CORRECTLY FAILING *****
(003) 555-1212
(103) 555-1212
(911) 555-1212
1-800-555-1234p
800x555x1234
+1 800 555x1234
***** FALSE POSITIVES *****
180055512345
1 800 5555 1234
+867 800 555 1234
1 (800)  555-1234
86 800 555 1212

Originally posted here: https://stackoverflow.com/questions/16699007/regular-expression-to-match-standard-10-digit-phone-number/39548938#39548938

Solution 8 - Regex

Here is the regex for Ethiopian Phone Number. For my fellow Ethiopian developers ;)

phoneExp = /^(^\+251|^251|^0)?9\d{8}$/;

It matches the following (restrict any unwanted character in start and end position)

  • +251912345678
  • 251912345678
  • 0912345678
  • 912345678

You can test it on this site regexr.

Solution 9 - Regex

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

Matches the following cases:

123-456-7890

(123) 456-7890

123 456 7890

123.456.7890

+91 (123) 456-7890

Solution 10 - Regex

Try this

\+?\(?([0-9]{3})\)?[-.]?\(?([0-9]{3})\)?[-.]?\(?([0-9]{4})\)?

It matches the following cases

  • +123-(456)-(7890)
  • +123.(456).(7890)
  • +(123).(456).(7890)
  • +(123)-(456)-(7890)
  • +123(456)(7890)
  • +(123)(456)(7890)
  • 123-(456)-(7890)
  • 123.(456).(7890)
  • (123).(456).(7890)
  • (123)-(456)-(7890)
  • 123(456)(7890)
  • (123)(456)(7890)

For further explanation on the pattern CLICKME

Solution 11 - Regex

The following regex matches a '+' followed by n digits


    var mobileNumber = "+18005551212";
    var regex = new RegExp("^\\+[0-9]*$");
    var OK = regex.test(mobileNumber);

    if (OK) {
      console.log("is a phone number");
    } else {
      console.log("is NOT a phone number");  
    }

Solution 12 - Regex

^+?\d{3}-?\d{2}-?\d{2}-?\d{3}$

You may try this....

How about this one....Hope this helps...

^(\\+?)\d{3,3}-?\d{2,2}-?\d{2,2}-?\d{3,3}$

Solution 13 - Regex

  ^[0-9\-\+]{9,15}$ 

would match 0+0+0+0+0+0, or 000000000, etc.

  (\-?[0-9]){7}

would match a specific number of digits with optional hyphens in any position among them.

What is this +077 format supposed to be?

It's not a valid format. No country codes begin with 0.

The digits after the + should usually be a country code, 1 to 3 digits long.

Allowing for "+" then country code CC, then optional hyphen, then "0" plus two digits, then hyphens and digits for next seven digits, try:

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

Oh, and {3,3} is redundant, simplifes to {3}.

Solution 14 - Regex

This regex matches any number with the common format 1-(999)-999-9999 and anything in between. Also, the regex will allow braces or no braces and separations with period, space or dash. "^([01][- .])?(\(\d{3}\)|\d{3})[- .]?\d{3}[- .]\d{4}$"

Solution 15 - Regex

Adding to @Joe Johnston's answer, this will also accept:

+16444444444,,241119933

(Required for Apple's special character support for dial-ins - https://support.apple.com/kb/PH18551?locale=en_US)

\(?\+[0-9]{1,3}\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{4}( ?-?[0-9]{3})? ?([\w\,\@\^]{1,10}\s?\d{1,10})?

Note: Accepts upto 10 digits for extension code

Solution 16 - Regex

/^(([+]{0,1}\d{2})|\d?)[\s-]?[0-9]{2}[\s-]?[0-9]{3}[\s-]?[0-9]{4}$/gm

https://regexr.com/4n3c4

Tested for

> +94 77 531 2412 > > +94775312412 > > 077 531 2412 > > 0775312412 > > 77 531 2412 > > > // Not matching > > 77-53-12412 > > +94-77-53-12412 > > 077 123 12345 > > 77123 12345

Solution 17 - Regex

JS code:

function checkIfValidPhoneNumber(input){
	"use strict";
	if(/^((\+?\d{1,3})?[\(\- ]?\d{3,5}[\)\- ]?)?(\d[.\- ]?\d)+$/.test(input)&&input.replace(/\D/g,"").length<=15){
		return true;
	} else { 
		return false;
	}
}

It may be primitive in terms of checking phone number, but it checks that input text is compliant with E.164 recommendation.

  1. Maximum phone length is 15 digits
  2. Country code consists of 1 to 3 digits, could be preceded with plus (could be omitted)
  3. Region (network) code consists of 3 to 5 digits (could be omitted but only if country code is omitted)

It allows some delimiters in phone number and around region code (.- ) For example:

> +7(918)000-12-34 > > 911 > > 1-23456-789.10.11.12

all are compliant with E.164 and validated

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNirView Question on Stackoverflow
Solution 1 - RegexGentView Answer on Stackoverflow
Solution 2 - RegexJoe JohnstonView Answer on Stackoverflow
Solution 3 - RegexJonathan LefflerView Answer on Stackoverflow
Solution 4 - RegexharoldolivieriView Answer on Stackoverflow
Solution 5 - RegexMatteo Bononi 'peorthyr'View Answer on Stackoverflow
Solution 6 - RegexfgeView Answer on Stackoverflow
Solution 7 - RegexLudovic AgatheView Answer on Stackoverflow
Solution 8 - RegexEndriyasView Answer on Stackoverflow
Solution 9 - RegexSanjaysinh ZalaView Answer on Stackoverflow
Solution 10 - Regexan-aplussView Answer on Stackoverflow
Solution 11 - RegexappsmaticsView Answer on Stackoverflow
Solution 12 - RegexPremanshuView Answer on Stackoverflow
Solution 13 - Regexg1smdView Answer on Stackoverflow
Solution 14 - Regexumbreon222View Answer on Stackoverflow
Solution 15 - RegexRaviView Answer on Stackoverflow
Solution 16 - RegexLasithdsView Answer on Stackoverflow
Solution 17 - RegexKogenbaView Answer on Stackoverflow