cakephp/localized — pull request #21 by g1smd (2012-11-15 00:36:52+00:00)
Title: Add validation for UK phone numbers.

Add validation for UK phone numbers.

Allows a wide variety of dial prefixes and NSN formats.

In this pattern, international phone numbers can begin +44, 00 44, 011 44, +44 (0), 00 44 (0), 011 44 (0), with or without parentheses around any part and with or without a space or hyphen after any part. The + before 44 is always optional in this pattern.

In this pattern, national numbers can begin 0, or the 0 can be omitted.

Parentheses are optional around the area code, with or without the 0 being present.

The remainder of the number must have 9 or 10 digits, and is matched in 2+8, 3+6, 3+7, 4+5, 4+6, 5+4 or 5+5 format with allowance for closing parentheses where the user thinks the area code ends. Don't worry if the user gets it wrong. It's the digits that are important. Additionally an optional hyphen or space is allowed between digits.

Allow the user to enter a phone number in any reasonable format, with optional parentheses, spaces and hyphens. Doesn't insist on balanced parentheses. It's the digits that are important, spaces and punctuation are not important.

"Be conservative in what you do, be liberal in what you accept from others." (Postel's Law)

The user input must be further cleaned and validated before it can be used.
"/^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?\+?|\+)?(44)\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0\)?[\s-]?\(?:?)?((1[1-9]|2[03489]|3[0347]|5[056]|7[04-9]|8[047]|9[018])(?:\)?[\s-]?\d){4}(?:[\s-]?\d){3,4})$/";

Extract the country code. The value '44' will be found in $1 if international format has been used on input, and is null if national format was used.

Extract the NSN from $2. Remove all parentheses, hyphens and spaces from the NSN and then perform more detailed validation on the NSN using the RegEx patterns suggested in http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers

Store the number in E.164 format if possible.

When the number needs to be displayed, format it in the right way using the additional RegEx patterns listed on the page above.

Example:
User enters '(00) +44 (0) (20) 3555 7788'.
UkValidation will return TRUE.

This means the dial prefix and country code and/or trunk prefix part of the number is plausible: 0, +44, +44 (0), 00 44, or one of several others, the phone number NSN has 9 or 10 digits and the first two digits of the NSN are ones that are used in the UK. However, the number might still not be in a valid area code.

Extract the country code and NSN part of the number using the RegEx pattern.
$1 = '44'
$2 = '20) 3555 7788'
Clean the NSN to '2035557788' by removing all spaces and punctuation.
Store the number as '+442035557788'.
Display the number as +44 20 3555 7788 or as (020) 3555 7788 using another set of RegEx patterns.