Simple JavaScript Regular Expression to Validate U.S Phone Numbers
Image credit: aussiegall
Continuing with our JavaScript regular expression series today we will discuss JavaScript regular expression to validate U.S phone numbers. Previously we talked about validating email, Social Security number, and zip code using JS regex.
I’ll show you how to use regular JavaScript expressions to validate U.S. phone numbers in today’s post.
Although in this article we are discussing the U.S. phone number format I am sure this can be applied to other phone number formats with little or no change.
Let’s begin by looking at the JavaScript code.
function validatePhoneNumber(elementValue){ var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/; return phoneNumberPattern.test(elementValue); }
Explanation:
The argument to this method is the phone number you want to validate.
In the method body we define a variable (‘phoneNumberPattern’) and assign a regular expression to it.
Phone Number format: The regular expression for phone number is
/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/
Let’s divide this regular expression in smaller fragments to make is easy to understand.
/^\(?: Means that the phone number may begin with an optional “(“.
(\d{3}): After the optional “(” there must be 3 numeric digits. If the phone number does not have a “(“, it must start with 3 digits. E.g. (308 or 308.
\)?: Means that the phone number can have an optional “)” after first 3 digits.
[- ]?: Next the phone number can have an optional hyphen (“-“) after “)” if present or after first 3 digits.
(\d{3}): Then there must be 3 more numeric digits. E.g (308)-135 or 308-135 or 308135
[- ]?: After the second set of 3 digits the phone number can have another optional hyphen (“-“). E.g (308)-135- or 308-135- or 308135-
(\d{4})$/: Finally, the phone number must end with four digits. E.g (308)-135-7895 or 308-135-7895 or 308135-7895 or 3081357895.
On the last line, we call the test method for our regular expression and pass the phone number as input. If the input phone number satisfies our regular expression, ‘test’ will return true otherwise it will return false. We return this value to the calling method.
suresh
October 14, 2009 @ 11:56 pm
Hi,
Ur javaacript for validating controls is very clear.and easy.
thank u
Abel Curtice
April 30, 2010 @ 6:51 pm
Very intereresting reading. thx
vertaa vippi
August 15, 2010 @ 8:17 am
Vertaa pikalainaaan päätös lainojen saada paras käsitellä – Kun verrataan lainat varmista voit pohtia huolellisesti todellinen vuosikorko sekä tsekkamall piilomaksuja.
prince appleton
February 2, 2011 @ 9:21 pm
call me pleas 7638071450
Sam
May 2, 2011 @ 11:56 am
Thank you! Excellent work!
raisch
May 31, 2011 @ 8:25 pm
In your second explanation, (d{3}) will match 000, 123, 199, 999, etc. which are all illegal U.S. area codes.
Use: ([^2-9]d{2}) for a better match, though it too will match 999 which is reserved for phone company use.
tnizz
August 2, 2011 @ 12:52 am
boo! doesnt work!
how to get ex back
August 22, 2011 @ 4:43 pm
What’s Going down i’m new to this, I stumbled upon this I’ve found It absolutely useful and it has helped me out loads. I’m hoping to give a contribution & assist other customers like its aided me. Good job.
Shena Nealon
September 12, 2011 @ 6:15 pm
I was wondering if perhaps you knew precisely where I possibly could obtain the XML feed for the blog.
kdniazi
September 27, 2011 @ 3:24 am
great article i was googling for last one hour but not found my required reg script. Because of this article i have able to write my own script for validate my required phone number format which is
+0?0-000-000-000?0
?= optional
var pattern = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
Thanks.
kdniazi
September 27, 2011 @ 3:26 am
frist one is for email vaidation and seconed is phone validatoan
var phoneNumberPattern = /^+(d{1,2}))?[- ](d{3})[- ](d{3})[- ](d{3,4})?$/
Miyagi
November 30, 2011 @ 1:04 pm
Crisp explanation! Thanks..
ram
January 12, 2012 @ 11:49 pm
fdddfdsddsf
Anand
January 17, 2012 @ 6:42 am
Very clear explanation on Regular expressions…. thanx….:)
Harpreet
January 17, 2012 @ 11:20 pm
Very good explanation..now i can make expressions myself
Robert
May 26, 2012 @ 3:04 pm
Your regex have really helped me in a mobile app I’m developing! Thanks so much! I’ll definitely being using them from this point on.
Darshan
June 21, 2012 @ 4:26 am
Very good explanation..
aum
September 18, 2012 @ 3:47 am
it’s an amezing code ,,,,,its just hel me .
so frnz use this code n enjoy
San
September 28, 2012 @ 3:56 am
Wow useful code set
Mike
January 16, 2013 @ 10:45 am
If you put a parenthesis at the beginning of the area code, then the second parenthesis must not be optional. Your regex breaks under this scenerio.
farmville cash generator 2.2 2012
August 1, 2014 @ 11:12 am
Hello it’s me, I am also visiting this web page on a regular
basis, this web page is in fact fastidious
and the users are truly sharing good thoughts.
feature points hack cydia
August 2, 2014 @ 5:18 am
What a data of un-ambiguity and preserveness of precious experience
on the topic of unpredicted emotions.
cheat generator farmville 2
August 6, 2014 @ 6:49 am
Please let me know if you’re looking for a author for your blog.
You have some really good posts and I believe I would be
a good asset. If you ever want to take some of the
load off, I’d really like to write some material for your blog in exchange for a link back to mine.
Please blast me an e-mail if interested. Thank you!
uberstrike generator money level up download
August 15, 2014 @ 12:20 pm
What’s up to all, how is all, I think every one is getting more from this website, and your views are good in support of new viewers.
Karbonn Sparkle V back covers in india
October 22, 2014 @ 1:18 pm
Thanks for every other wonderful post. The place else could
anybody get that type of info in such an ideal means of writing?
I’ve a presentation next week, and I’m on the search for
such information.
Mike G
December 11, 2014 @ 10:44 am
I was int0 SQL DBA 2008 R 2 and i have switched to Java developer… I was stuck the last 2 days until I ran into this.. Ty so much for the explanation..