Validating input in java script
So far, the regular expression matches any 10-digit number.
If you want to limit matches to valid phone numbers according to the North American Numbering Plan, here are the basic rules: Beyond the basic rules just listed, there are a variety of reserved, unassigned, and restricted phone numbers.
To allow matching phone numbers that omit the local area code, enclose the first group of digits together with its surrounding parentheses and following separator in an optional, noncapturing group: , with an empty set of parentheses.
To work around this, add code outside the regex that checks whether group 1 matched any text, and adjust the replacement text accordingly.
Note that the first word boundary token appears after the optional, opening parenthesis.
This is important because there is no word boundary to be matched between two nonword characters, such as the opening parenthesis and a preceding space character.
More information is available at See Recipe 3.5 for help implementing this regular expression with other programming languages. This regular expression follows the international phone number notation specified by the Extensible Provisioning Protocol (EPP).
The rules and conventions used to print international phone numbers vary significantly around the world, so it’s hard to provide meaningful validation for an international phone number unless you adopt a strict format. # Repeat the preceding group between 6 and 14 times. EPP is a relatively recent protocol (finalized in 2004), designed for communication between domain name registries and registrars.
Since a question mark is used after each separator, the phone number digits are allowed to run together.For more than 100 detailed regular expression recipes that include equal coverage for eight programming languages (C#, Java, Java Script, Perl, PHP, Python, Ruby, and VB. We’ll explain more about the question mark after discussing the other types of tokens in this regular expression.