CancelImage Upload

How to validate Phone Numbers using Regular Expressions

Validating a phone number is easy. That is if you merely want to make sure that the user has indeed entered a number, and not used any alphanumeric letters.

When you want to take things a step further however things can get slightly complex. Especially since every country has its own phone number style, and even among that style the number of expected digits from different regions can differ, or even change at any given point in time.

So taking the above into account, and then adding mobile phones into the mix you might think to yourself that you are better of with a general regular expression that checks for digits:

<script language="JavaScript">

function phone_validate(phone)
{

       var phoneReg = /^([0-9]){7,15}$/;

       if(phoneReg.test(phone) == false)
       {

              alert("Phone number is not yet valid.");

       }
       else
       {

              alert("You have entered a valid phone number!");

       }

}

</script>

The above JavaScript code performs a Phone Number validation in it's most simplistic form. The phone number will be deemed valid if it is between 7 and 15 digits long, consisting of digits between 0 and 9:

/^([0-9]){7,15}$/

For more complex number validation regular expressions you will need to look up the format for the country's phone numbers you wish to validate, and then adjust the min and max numbers accordingly!

Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com