#1
  1. Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Aug 2007
    Posts
    65
    Rep Power
    17

    Simple US telephone number validator


    I've seen many different types of telephone validating scripts, but nothing simple enough for my tastes. I came up with this after a little experimentation and thought I'd put it here in the hope someone else might find it useful.

    Code:
    <html>
    <body>
    <script language="javascript" type="text/javascript">
    function checkPhone(chk){
    	if(!chk.value.replace(/[^0-9]/g,"").match(/^[2-9]{1}[0-9]{2}[2-9]{1}[0-9]{2}[0-9]{4}$/)){
    		alert("Bad phone number. Try again.");
    		chk.focus();
    	}
    }
    </script>
    <body>
    <input type="text" name="phone" id="phone" size="20" />
    <input type="button" value="Check" onclick="checkPhone(phone);" />
    </body>
    </html>
    This removes all non-numeric characters (any spacing) and checks that the area code and prefix don't start with a 1 or 0 (which are not valid phone numbers).

    What I like most is that it is smaller and more powerful than most other telephone validation scripts I've come across.
  2. #2
  3. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Lightbulb Re: Simple us telephone number validator


    I will add something to that. Don't just check if it's a valid number. It's better to check the Country and the state of the number. I have a javascript which can check the location and the state (if its a US number) of the phone number. It can also detect Toll free and premium numbers. I wrote a blog on this. You can download the whole project at the end of the post. I hope it helps :-)
    Code:
    http://ipowerblogger.com/javascript/validate-phone-number-location-using-javascript/
  4. #3
  5. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    Sep 2011
    Posts
    105
    Rep Power
    7
    Originally Posted by bryceowen
    I've seen many different types of telephone validating scripts, but nothing simple enough for my tastes. I came up with this after a little experimentation and thought I'd put it here in the hope someone else might find it useful.

    Code:
    <html>
    <body>
    <script language="javascript" type="text/javascript">
    function checkPhone(chk){
    	if(!chk.value.replace(/[^0-9]/g,"").match(/^[2-9]{1}[0-9]{2}[2-9]{1}[0-9]{2}[0-9]{4}$/)){
    		alert("Bad phone number. Try again.");
    		chk.focus();
    	}
    }
    </script>
    <body>
    <input type="text" name="phone" id="phone" size="20" />
    <input type="button" value="Check" onclick="checkPhone(phone);" />
    </body>
    </html>
    This removes all non-numeric characters (any spacing) and checks that the area code and prefix don't start with a 1 or 0 (which are not valid phone numbers).

    What I like most is that it is smaller and more powerful than most other telephone validation scripts I've come across.
    Hello,

    Use the below links to validate the number

    http://vikku.info/codesnippets/javascript/us-phone-number-validation-by-adding-hypen-automatically-using-javascript/

    http://www.zparacha.com/phone_number_regex/

    http://blog.stevenlevithan.com/archives/validate-phone-number
  6. #4
  7. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jun 2010
    Posts
    22
    Rep Power
    0
    I would advise against using JS for validation only. It is fine as a pre-validator, but JS can be turned off or even altered.

Similar Threads

  1. Auto number generator..
    By wackyflik in forum ASP Development
    Replies: 2
    Last Post: October 8th, 2004, 04:37 AM
  2. a very loooong number (simple question)
    By sjakie in forum ASP Development
    Replies: 4
    Last Post: October 3rd, 2004, 10:02 AM
  3. Changing Starting number of Auto Number
    By motoxeryz125 in forum Microsoft Access Help
    Replies: 1
    Last Post: August 11th, 2004, 11:41 AM
  4. Simple combo box/text box issue, difficult description (part 1)
    By etalent in forum Microsoft Access Help
    Replies: 1
    Last Post: March 29th, 2004, 05:16 PM
  5. Datagrid control return wrong page number.
    By mex in forum .NET Development
    Replies: 0
    Last Post: February 26th, 2003, 10:48 AM

IMN logo majestic logo threadwatch logo seochat tools logo