Phone Number Validation using Regular Expression
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 do this.
Example of phone validation
Cut and Paste Code
Cut 'n' paste code for the above Example
- 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