Welcome Guest, Not a member yet? Register   Sign In
UK Phone Number Validation (regexp trickle!)
#2

[eluser]g1smd[/eluser]
Only very small parts of the 02 and 05 number ranges are in use, and little more in the 03 range. I'd use more specific patterns.
The 071, 072 and 073 ranges are not yet in use in the UK.


Optimising RegEx patterns can have an appreciable impact on performance.

Quote:(01|02|03|05|070|071|072|073|074|075|07624|077|078|079
Having found the 0 once, why keep on finding it again and again?
Likewise for mobiles, having found the 07 once, why find it again and again?
Try:
Code:
(0([123]|5[056]|7([45789]|624)))


Likewise,
Quote:(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)
having found the 011 for 0113, why have to find it again for 0114 onwards?
Having found the 01 for 0121, why have to find it again for 0131 onwards?
Try:
Code:
(01(1[3-8]|[2-6]1))
or
Code:
(01(1[3-8]|[2-69]1))
including 0191.



Users should be allowed to enter numbers in any format they want to, either in international format with country code or in national format but with an ISO 3166 country identifier for clarification.

The data should then be cleaned:
- remove any access code (+, 00, 011, etc), if present,
- remove the country code, if present, and store it for later use,
- generate the country code from the entered ISO 3166 country code, if present,
- use a default country code for national numbers where no country is specified,
- remove and separately store any extension number details,
- remove all spaces and non-digits from whatever is left,
- remove any leading 0 if present (except for Italy and one or two other countries where it should be left in place),
- check the number length is valid for the initial digit(s), and
- identify the number type from the initial digit(s).
Finally, format the number according to the formating rules for the identified number type and then display it. International format is always preferred.

Separating the validation routines from the formatting routines makes for code that is much easier to maintain.



I have generated a complete set of selection, validation and formatting RegEx patterns for every UK number range and number type. It's too long to reproduce here and it would be difficult to maintain multiple copies. This can be seen at:
http://www.aa-asterisk.org.uk/index.php/...ne_Numbers

The first section of the page shows patterns that can be used to validate the number is likely a GB phone number with a variety of dial prefixes, and for extracting the NSN part of the number.
The second section of the page shows patterns that can be used to identify whether the number is valid and the number type. The patterns work with the previously extracted NSN part.
The third section shows the various patterns used for formatting each number range.

The code works to the formats listed at:
http://www.aa-asterisk.org.uk/index.php/Number_format
noting the complexities detailed in:
http://www.aa-asterisk.org.uk/index.php/01_numbers
http://www.aa-asterisk.org.uk/index.php/02_numbers
http://www.aa-asterisk.org.uk/index.php/Mixed_areas
and other pages.


I'm also the UK metadata editor for the Google libphonenumber project over at:
http://code.google.com/p/libphonenumber/
The xml metadata file there has number length, validation and formatting information for every country.


Additionally, if you're interested in selecting the right length and initial digits for local numbers, details for every UK area code are here:
http://www.aa-asterisk.org.uk/index.php/...ne_numbers



Messages In This Thread
UK Phone Number Validation (regexp trickle!) - by El Forum - 02-03-2011, 07:09 AM
UK Phone Number Validation (regexp trickle!) - by El Forum - 07-07-2012, 06:52 PM



Theme © iAndrew 2016 - Forum software by © MyBB