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.
regex - Regular Expression to reformat a US phone number in Javascript - Stack Overflow
Javascript Regex - What to use to validate a phone number? - Stack Overflow
regex - Validate phone number using javascript - Stack Overflow
How to Match a Phone Number with Regex and JavaScript
Videos
Assuming you want the format "(123) 456-7890":
function formatPhoneNumber(phoneNumberString) {
var cleaned = ('' + phoneNumberString).replace(/\D/g, '');
var match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/);
if (match) {
return '(' + match[1] + ') ' + match[2] + '-' + match[3];
}
return null;
}
Here's a version that allows the optional +1 international code:
function formatPhoneNumber(phoneNumberString) {
var cleaned = ('' + phoneNumberString).replace(/\D/g, '');
var match = cleaned.match(/^(1|)?(\d{3})(\d{3})(\d{4})$/);
if (match) {
var intlCode = (match[1] ? '+1 ' : '');
return [intlCode, '(', match[2], ') ', match[3], '-', match[4]].join('');
}
return null;
}
formatPhoneNumber('+12345678900') // => "+1 (234) 567-8900"
formatPhoneNumber('2345678900') // => "(234) 567-8900"
var x = '301.474.4062';
x = x.replace(/\D+/g, '')
.replace(/(\d{3})(\d{3})(\d{4})/, '(
2-$3');
alert(x);
Try this code
HTML Code
<input type="text" id="phone"/>
JS Code
$("#phone").blur(function() {
var regexp = /^[\s()+-]*([0-9][\s()+-]*){6,20}
("#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
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! * 😉
So, first, I should point out that requiring US-formatted telephone numbers is pretty restrictive; international numbers can have very different rules. That said, this regex should meet your needs:
/(?:^|\D)\(([2-9])(?:\d(?!\1)\d|(?!\1)\d\d)\)\s*[2-9]\d{2}-\d{4}/
First,to prevent matching things that end with a valid phone number but have extra junk up front, we match either the start of the string (^) or a non-digit (\D). Then the opening parenthesis of the area code, (\().
Then we match the first digit of the area code, [2-9].
Then we match either any digit (\d) followed by any digit except the first one ((?!\1)\d), or the other way around ((?!\1)\d\d). This keeps the area code from being three identical digits.
Then we close the parens (\)), allow (but don't require) space (\s*) before the first digit of the prefix ([2-9] again), followed by any two digits (\d{2}), a hyphen, and any four digits (\d{4}).
Let's go by parts, you got three conditions:
- ALL 9 digits being 0 is not allowed
- The area code (first 3 digits) cannot be the same digit,
- The 1st and 4th digit cannot be 0 or 1.
Condition 1 is redundant if you consider condition 3; A simple regex not considering condition 2 is:
/^\([2-9]\d\d\) [2-9]\d\d-\d{4}$/
Assuming you want parenthesis and spaces - (555) 555-5555
Explanation:
- \d matches any digit
- [2-9] matches any character from 2 to 9
- space and dash are literals - match spaces and dash
- {4} is a quantifier - will match 4 digits in this case
- ( and ) are escaped literals - will match ( and ) respectively
Now if we want to consider condition number 2 in our expression, we use
- a negative lookahead ?!
- a capturing group () and
- a back reference \1.
Read some regex reference if you want to fully understand those. The full expression is:
^\(([2-9])(?!\1\1)\d\d\) [2-9]\d\d-\d{4}$
Based on your samples, try this:
^(?:\+?\d{2}[ -]?\d{3}[ -]?\d{5}|\d{4})$
It will match all the correct values.
DESCRIPTION:

DEMO:
http://regexr.com?340nf
Just to help you build some concepts.
The following regex would match the first seven inputs you provided.
/^\+?\d{2}[- ]?\d{3}[- ]?\d{5}$/
\+? would match a + sign. The ? in it makes the + sign optional.
\d{2} matches two digits
[- ]? matches either a - or a (space). ? makes the occurrence of - or (space) optional.
\d{5} then matches 5 digits.
^ and $ are the start and end anchors.