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.