Javascript function for validating phone number

Rated 3.86/5 based on 909 customer reviews

In the following example user enters a phone number.

When the user presses Enter, than java Script checks that phone number is valid or not.

This java Script code determine whether a user entered phone number in a common format that includes 0999999999, 099-999-999, (099)-999-9999, (099)9999999, 0, 099 999-9999, (099) 999-9999, 099.999.9999 and all related combinations For this we use this regular expression: /^\(?

Recently i have posted how to validate email address in javascript, how to validate mobile number and email address in jquery and not i will show you how to validate mobile number using simple javascript or using regular expression in javascript.

With all of this in mind, let’s look at the regular expression again after breaking it into its pieces. : # Group but don't capture: [0-9] # Match a digit. ITU-T Recommendation E.123 (“Notation for national and international telephone numbers, e-mail addresses and web addresses”) can be downloaded at

Because this version is written using free-spacing style, the literal space character has been replaced with ^ # Assert position at the beginning of the string. ITU-T Recommendation E.164 (“The international public telecommunication numbering plan”) can be downloaded at RFC 5733 defines the syntax and semantics of EPP contact identifiers, including international phone numbers. Techniques used in the regular expressions in this recipe are discussed in Chapter 2.

If the phone number was invalid, we would want to return false to cancel form submission as well as informing the user of the errors of their ways.

There is two ways given below for validation of mobile numbers first normal way and another through regular expression.

You can use them to constrain input, apply formatting rules, and check lengths.

A regular expression can easily check whether a user entered something that looks like a valid phone number.

This notation requires that international phone numbers include a leading plus sign (known as the ), and allows only spaces to separate groups of digits. EPP is a relatively recent protocol (finalized in 2004), designed for communication between domain name registries and registrars.

Although the tilde character (~) can appear within a phone number to indicate the existence of an additional dial tone, it has been excluded from this regular expression since it is merely a procedural element (in other words, it is not actually dialed) and is infrequently used. It is used by a growing number of domain name registries, including .

Leave a Reply