Javascript Phone Number Validation
International Phone No. Validation in Forms
In forms when asking for phone numbers fields it is a good idea to use client side validation along with your programming language validation. The following example shows how you can validate an International Phone number in your forms using Javascript.
International Phone Number Format
Most telephone networks nowadays are interconnected in the international telephone network, where the format of telephone numbers is standardized by ITU-T in the recommendation E.164, which specifies that the entire number should be 15 digits or shorter, and begin with a country prefix followed by an area code or city code and the subscriber number, which might consist of the code for a particular telephone exchange. In writing an international telephone number should start with a plus sign ("+") and the country code.the The "+" stands for the international call prefix chosen by the country the call is being made from. The general standard is 00 and this has been implemented by a large number of countries. For North American Numbering Plan (NANP) countries (including the United States and Canada) the call prefix is "011" for other countries and "1" for countries inside the NANP. Click here for NANP phone number validation and US only phone number validation.
Example
Cut and Paste Code
Cut 'n' paste code for the above Example
Explanation of the Code
The JavaScript has the following main functions:
- Function checkInternationalPhone
is used to verify if the given value is a possible valid international
phone number : This function first removes all non-digit characters
which are allowed in phone numbers. These delimiters are declared
in the lines (found in the beginning of the code) :
var phoneNumberDelimiters = "()- "
var validWorldPhoneChars = phoneNumberDelimiters + "+"
Now that all valid delimiters are removed we just check if the remaining value is an integer and that it has at least a certain number of digits (given by the variable 'minDigitsInIPhoneNumber').
- Function ValidateForm is used to make sure that the phone number field is not blank and that it is a valid phone number on form submission