Miscellaneous Code

  Home arrow Miscellaneous Code arrow JavaScript/PHP US phone number validat...
MISCELLANEOUS CODE

JavaScript/PHP US phone number validation
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 21
    2003-11-05

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    Javascript and PHP versions of a function to verify a US phone number (including parentheses, dashes and spaces).

    Overview: this function will validate (but NOT verify) any US phone number. It may seem trivial, but many people have run into trouble with this problem because of the tendency to use parentheses () and dashes - in the number, making a simple numerical check insufficient. Often, designers must resort to using three separate fields for the number where only one is needed.

    The PHP version will return the 10-digit phone number if it is valid; see code for full description.

    By : mugane

    ////////////////////////////////////////
    // JavaScript function to validate US
    // phone number:
    //
    // (c) 2003
    // No restrictions have been placed on the use
    // of this code
    //
    // Updated Friday Jan 9 2004 to optionally ignore
    // the area code:
    //
    // Input: a single string parameter and an
    // optional boolean variable (default=true)
    // Output: boolean true(1) or false(0)
    //
    // The function will return true for any
    // alphanumeric string with the following
    // sequence of characters:
    // any number of spaces [optional], a single
    // open parentheses [optional], any number of
    // spaces [optional], 3 digits (area
    // code), any number of spaces [optional], a
    // single close parentheses [optional], a single
    // dash [optional], any number of spaces
    // [optional], 3 digits, any number of spaces
    // [optional], a single dash [optional], any
    // number of spaces [optional], 4 digits, any
    // number of spaces [optional].
    //
    ////////////////////////////////////////
    function check_usphone(phonenumber,useareacode)
    {
    if(!useareacode)useareacode=1;
    if((phonenumber.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null) && ((useareacode!=1) && (phonenumber.match(/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null))) return false;
    return true;
    }

    ////////////////////////////////////////
    //
    // PHP function to validate US phone number:
    // (c) 2003
    // No restrictions have been placed on
    // the use of this code
    //
    // Updated Friday Jan 9 2004 to optionally ignore
    // the area code:
    //
    // Input: a single string parameter and an
    // optional boolean variable (default=true)
    // Output: 10 digit telephone number
    // or boolean false(0)
    //
    // The function will return the numerical part
    // of the alphanumeric string parameter with
    // the following sequence of characters:
    // any number of spaces [optional], a single
    // open parentheses [optional], any number of
    // spaces [optional], 3 digits (area
    // code), any number of spaces [optional], a
    // single close parentheses [optional], a single
    // dash [optional], any number of spaces
    // [optional], 3 digits, any number of spaces
    // [optional], a single dash [optional], any
    // number of spaces [optional], 4 digits, any
    // number of spaces [optional]:
    //
    ////////////////////////////////////////
    function VALIDATE_USPHONE($phonenumber,$useareacode=true)
    {
    if ( preg_match("/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/",$phonenumber) || (preg_match("/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/",$phonenumber) && !$useareacode)) return eregi_replace("[^0-9]", "", $phonenumber);
    return false;
    }
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap