My regex of choice is:
/^[\+]?[0-9]{0,3}\W?+[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im
Valid formats:
(123) 456-7890
(123)456-7890
123-456-7890
123.456.7890
1234567890
+31636363634
075-63546725
+1 (415)-555-1212
+1 (123) 456-7890
+1 (123)456-7890
+1 123-456-7890
+1 123.456.7890
+1 1234567890
+1 075-63546725
+12 (415)-555-1212
+12 (123) 456-7890
+12 (123)456-7890
+12 123-456-7890
+12 123.456.7890
+12 1234567890
+123 075-63546725
+123 (415)-555-1212
+123 (123) 456-7890
+123 (123)456-7890
+123 123-456-7890
+123 123.456.7890
+123 1234567890
+123 075-63546725
+1(415)-555-1212
+1(123) 456-7890
+1(123)456-7890
+1123-456-7890
+1123.456.7890
+11234567890
+1075-63546725
+12(415)-555-1212
+12(123) 456-7890
+12(123)456-7890
+12123-456-7890
+12123.456.7890
+121234567890
+123075-63546725
+123(415)-555-1212
+123(123) 456-7890
+123(123)456-7890
+123123-456-7890
+123123.456.7890
+1231234567890
+123075-63546725
Answer from EeeeeK on Stack OverflowMy regex of choice is:
/^[\+]?[0-9]{0,3}\W?+[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im
Valid formats:
(123) 456-7890
(123)456-7890
123-456-7890
123.456.7890
1234567890
+31636363634
075-63546725
+1 (415)-555-1212
+1 (123) 456-7890
+1 (123)456-7890
+1 123-456-7890
+1 123.456.7890
+1 1234567890
+1 075-63546725
+12 (415)-555-1212
+12 (123) 456-7890
+12 (123)456-7890
+12 123-456-7890
+12 123.456.7890
+12 1234567890
+123 075-63546725
+123 (415)-555-1212
+123 (123) 456-7890
+123 (123)456-7890
+123 123-456-7890
+123 123.456.7890
+123 1234567890
+123 075-63546725
+1(415)-555-1212
+1(123) 456-7890
+1(123)456-7890
+1123-456-7890
+1123.456.7890
+11234567890
+1075-63546725
+12(415)-555-1212
+12(123) 456-7890
+12(123)456-7890
+12123-456-7890
+12123.456.7890
+121234567890
+123075-63546725
+123(415)-555-1212
+123(123) 456-7890
+123(123)456-7890
+123123-456-7890
+123123.456.7890
+1231234567890
+123075-63546725
First off, your format validator is obviously only appropriate for NANP (country code +1) numbers. Will your application be used by someone with a phone number from outside North America? If so, you don't want to prevent those people from entering a perfectly valid [international] number.
Secondly, your validation is incorrect. NANP numbers take the form NXX NXX XXXX where N is a digit 2-9 and X is a digit 0-9. Additionally, area codes and exchanges may not take the form N11 (end with two ones) to avoid confusion with special services except numbers in a non-geographic area code (800, 888, 877, 866, 855, 900) may have a N11 exchange.
So, your regex will pass the number (123) 123 4566 even though that is not a valid phone number. You can fix that by replacing \d{3} with [2-9]{1}\d{2}.
Finally, I get the feeling you're validating user input in a web browser. Remember that client-side validation is only a convenience you provide to the user; you still need to validate all input (again) on the server.
TL;DR don't use a regular expression to validate complex real-world data like phone numbers or URLs. Use a specialized library.
Phone Number Regular Expression Validation - JavaScript - SitePoint Forums | Web Development & Design Community
How to Match a Phone Number with Regex and JavaScript
Javascript Regex - What to use to validate a phone number? - Stack Overflow
regex - Validate phone number using javascript - Stack Overflow
What’s the best way to clean user input before regex?
Can this regex validate all global formats?
How can I create a regular expression that only allows certain characters (digits, +, (), -,., and spaces) in phone numbers?
Videos
I'll be honest, the first time I saw a regular expression, it was a scary experience. It looks like a weird alien language! I thought to myself: "I've spent months learning programming and now i gotta learn this seemly super complex language!?"
However, once I sat down to actually learn regex, I discovered it's not super hard, once you learn the syntax.
Why should I even bother Learning Regex?
As you start coding more, and more, it really comes in handy in all types of situations, and not just to valid phone numbers, and email addresses. It's very helpful when extract data from logs, messy JSON data from API calls, and many other situations.
I'm going to teach you how to valid a phone number with 1 line of code, with 1 regular expression. **Validating a phone number WITHOUT regex becomes an obnoxious leetcode question. ** 😧
Why is validating a phone number so complex?
Let's say you have a form on your website to collect a phone number to spa-, I mean, SMS your subscribers, there are a bunch of different ways you could submit their phone numbers.
All of these are VALID US based numbers:
-
202-515-5555 -
202 515 5555 -
(202)515 5555 -
1 202 515 5555 -
2025155555 -
1-202-515-5555 -
1202-515-5555 -
etc
There are more valid combinations I didn't list, but you get the idea! Validating every combo because a nasty coding problem. *But not if you're using regex to validate it! * 😉
Try this code
HTML Code
<input type="text" id="phone"/>
JS Code
$("#phone").blur(function() {
var regexp = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/
var no = $("#phone").val();
if (!regexp.test(no) && no.length < 0) {
alert("Wrong phone no");
}
});
See A comprehensive regex for phone number validation
Quick cheat sheet
- Start the expression:
/^ - If you want to require a space, use:
[\s]or\s - If you want to require parenthesis, use:
[(]and[)]. Using\(and\)is ugly and can make things confusing. - If you want anything to be optional, put a
?after it - If you want a hyphen, just type
-or[-]. If you do not put it first or last in a series of other characters, though, you may need to escape it:\- - If you want to accept different choices in a slot, put brackets around the options:
[-.\s]will require a hyphen, period, or space. A question mark after the last bracket will make all of those optional for that slot. \d{3}: Requires a 3-digit number: 000-999. Shorthand for[0-9][0-9][0-9].[2-9]: Requires a digit 2-9 for that slot.(\+|1\s)?: Accept a "plus" or a 1 and a space (pipe character,|, is "or"), and make it optional. The "plus" sign must be escaped.- If you want specific numbers to match a slot, enter them:
[246]will require a 2, 4, or 6.[77|78]will require 77 or 78. $/: End the expression
JavaScript to validate the phone number:
function phonenumber(inputtxt) {
var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if(inputtxt.value.match(phoneno)) {
return true;
}
else {
alert("message");
return false;
}
}
The above script matches:
XXX-XXX-XXXX
XXX.XXX.XXXX
XXX XXX XXXX
If you want to use a + sign before the number in the following way
+XX-XXXX-XXXX
+XX.XXXX.XXXX
+XX XXXX XXXX
use the following code:
function phonenumber(inputtxt) {
var phoneno = /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
if(inputtxt.value.match(phoneno)) {
return true;
}
else {
alert("message");
return false;
}
}
This regular expression /^(\([0-9]{3}\)\s*|[0-9]{3}\-)[0-9]{3}-[0-9]{4}$/ validates all of the following:
'123-345-3456';
'(078)789-8908';
'(078) 789-8908'; // Note the space
To break down what's happening:

- The group in the beginning validates two ways, either
(XXX)orXXX-, with optionally spaces after the closing parenthesis. - The part after the group checks for
XXX-XXX