Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here



How to validate a US Phone Number on the fly by EdwardMartinIII
Posted: 17 Mar 03 (Edited 17 Mar 03)

U.S. Phone numbers are generally ten digits.  However, people format 'em all kinds of different ways (xxx-xxx-xxxx, (xxx) xxx-xxxx, xxx.xxx.xxxx, etc.) and there's nothing more annoying than to try and guess how the Programmer wanted the number formatted.  Likewise, there's no reason to insist that the User format a number a certain way.

Ideally, the User shouldn't have to worry about formatting -- the software should do it for them, throwing up an error only if there's some problem.

With that in mind, this sample takes an input string, strips everything out except digits, throws an error up if the count <> 10, and formats a 10-count number according to the following standard: (xxx) xxx-xxxx.  Obviously, you can modify the js code to format it however you wish.  The idea here is that this is one part of the validation sequence for a data entry page.  Eventually, there is a confirmation page and, like most people, we won't care how the number is formatted, just that all the digits are there.

This is the HTML, which I saved as Sample.html:

<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></meta>
    <title>JavaScript Sample</title>
    <script src="Sample.js" type="text/javascript"></script>
    <form name="CustomerData">
      <input type="text" id="PhoneNumber"></input><input type="button" value="Parse this US number" onclick="CustomerData.PhoneNumber.value=ParseUSNumber(CustomerData.PhoneNumber.value)"></input>

and this is the external js file, which I saved as Sample.js and stored in the same directory:

function ParseUSNumber(PhoneNumberInitialString)
    var FmtStr="";
    var index = 0;
    var LimitCheck;

    LimitCheck = PhoneNumberInitialString.length;
    while (index != LimitCheck)
        if (isNaN(parseInt(PhoneNumberInitialString.charAt(index))))
          { }
          { FmtStr = FmtStr + PhoneNumberInitialString.charAt(index); }
        index = index + 1;
    if (FmtStr.length == 10)
        FmtStr = "(" + FmtStr.substring(0,3) + ") " + FmtStr.substring(3,6) + "-" + FmtStr.substring(6,10);
        alert("United States phone numbers must have exactly ten digits.");
    return FmtStr;

There is no facility for entering extensions, but seeing as how extensions can be basically anything, if you want extensions, you might consider dropping validation altogether and just collect the string as entered by the User.

Another weakness of which you should be aware is that it'll successfully parse anything that happens to have ten digits, from "(555) 555-1212" to "Billy ate the heads off 55 bats, 12 dogs, 4 gerbils and a 9-headed cow before breaking legs 1, 2 and 3 of its 4 legs."  So, if your Users are inclined to insert such data, you might be aware of the possibility...

Validation for International numbers depends, of course, on the country in question.

It is my hope that this bit of validation code prove useful to you.


Edward Martin III

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close