Validate U.S Phone Numbers using JavaScript Regular expression.


Photo credit: aussiegall

Continuing with our JavaScript regular expression series today we will discuss JavaScript regular expression to validate U.S phone number. Previously we talked about validating email , Social Security number and zip code using JS regex.

In today’s post I’ll show you how to use JavaScript regular expression to validate U.S phone number.


Although in this article we are discussing 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 final line we call 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.


If you enjoyed this post, make sure you subscribe to my RSS feed!

26 Thoughts on “Validate U.S Phone Numbers using JavaScript Regular expression.

  1. Hi,
    Ur javaacript for validating controls is very clear.and easy.

    thank u

  2. Abel Curtice on April 30, 2010 at 6:51 pm said:

    Very intereresting reading. thx

  3. Vertaa pikalainaaan päätös lainojen saada paras käsitellä – Kun verrataan lainat varmista voit pohtia huolellisesti todellinen vuosikorko sekä tsekkamall piilomaksuja.

  4. prince appleton on February 2, 2011 at 9:21 pm said:

    call me pleas 7638071450

  5. Thank you! Excellent work!

  6. raisch on May 31, 2011 at 8:25 pm said:

    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.

  7. boo! doesnt work!

  8. 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.

  9. I was wondering if perhaps you knew precisely where I possibly could obtain the XML feed for the blog.

  10. 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.

  11. frist one is for email vaidation and seconed is phone validatoan
    var phoneNumberPattern = /^\+(\d{1,2})\)?[- ](\d{3})[- ](\d{3})[- ](\d{3,4})?$/

  12. Crisp explanation! Thanks..

  13. fdddfdsddsf

  14. Very clear explanation on Regular expressions…. thanx….:)

  15. Very good explanation..now i can make expressions myself

  16. 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.

  17. Darshan on June 21, 2012 at 4:26 am said:

    Very good explanation..

  18. it’s an amezing code ,,,,,its just hel me .

    so frnz use this code n enjoy

  19. Wow useful code set

  20. 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.

  21. 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.

  22. What a data of un-ambiguity and preserveness of precious experience
    on the topic of unpredicted emotions.

  23. 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!

  24. 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.

  25. 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.

  26. 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..

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation