Perl validating numbers
Beyond the basic rules just listed, there are a variety of reserved, unassigned, and restricted phone numbers.Unless you have very specific needs that require you to filter out as many phone numbers as possible, don’t go overboard trying to eliminate unused numbers.Since a question mark is used after each separator, the phone number digits are allowed to run together. The NANP is the telephone numbering plan for the countries that share the country code “1.” This includes the United States and its territories, Canada, Bermuda, and 17 Caribbean nations.It excludes Mexico and the Central American nations.Thanks to the international phone numbering plan (ITU-T E.164), phone numbers cannot contain more than 15 digits. The significance of this is that EPP-style international phone numbers are increasingly used and recognized, and therefore provide a good alternative format for storing (and validating) international phone numbers.The shortest international phone numbers in use contain seven digits. EPP-style phone numbers use the format Recipe 4.2 provides more options for validating North American phone numbers.
Recipe 2.1 explains which special characters need to be escaped.
A regular expression can easily check whether a user entered something that looks like a valid phone number. Thus, the sequences that enclose the first group of digits match literal parenthesis characters.
By using capturing groups to remember each set of digits, the same regular expression can be used to replace the subject text with precisely the format you want. Both are followed by a question mark, which makes them optional.
Character classes allow you to match any one out of a set of characters.
is another character class, one that allows any one of three separators.