1

Has anyone got a good regex that will match on both UK and USA numbers?

It would need to cater for uk prefixes like +44 and also the US equivalent.

Does anyone know of such an expression?

user229044
  • 232,980
  • 40
  • 330
  • 338
dagda1
  • 26,856
  • 59
  • 237
  • 450
  • Reference: [North American Numbering Plan](http://en.wikipedia.org/wiki/North_American_Numbering_Plan?title=North_American_Numbering_Plan) – Pekka Oct 25 '10 at 12:07
  • 2
    Looking at both UK and US numbering systems and the rapidity at which both change (to accommodate for growing cities, new cell networks and such), I would recommend a very simple regex that checks for the country prefix (+44 or +1) and then x number of digits (officially up to 19 max, but extensions might add more). – Pekka Oct 25 '10 at 12:08
  • 1
    This question comes up almost every week. Please do a search first. – Ether Oct 25 '10 at 16:14
  • possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – mmmmmm Feb 07 '12 at 14:47
  • have a look at http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – Kyle Hudson Oct 25 '10 at 12:06

3 Answers3

0

You could use the following to validate UK mobile numbers:

<?php
function valid_UK_mobile($gsm) {
    return preg_match("/^00447\d{9}$|^07\d{9}$|^447\d{9}$|^[+]447\d{9}$/", $gsm); // +447XXXXXXXXX, 447XXXXXXXXX, 07XXXXXXXXX, 00447XXXXXXXXX
}
echo (valid_UK_mobile("+447000000000") ? 'OK' : 'NOT OK') . '<br />'; //success
echo (valid_UK_mobile("447000000000") ? 'OK' : 'NOT OK') . '<br />'; //success
echo (valid_UK_mobile("07000000000") ? 'OK' : 'NOT OK') . '<br />'; //success
echo (valid_UK_mobile("00447000000000") ? 'OK' : 'NOT OK') . '<br />'; //success
echo (valid_UK_mobile("00440000000000") ? 'OK' : 'NOT OK') . '<br />'; // fail
?>

something weird with stackoverflow, the code - http://paste-it.net/public/o060a91/

Kyle Hudson
  • 898
  • 1
  • 14
  • 26
0

You could even use:

^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$

http://regexlib.com/REDetails.aspx?regexp_id=495

or

^(?:(?<1>[(])?(?<AreaCode>[2-9]\d{2})(?(1)[)])(?(1)(?<2>[ ])|(?:(?<3>[-])|(?<4>[ ])))?)?(?<Prefix>[1-9]\d{2})(?(AreaCode)(?:(?(1)(?(2)[- ]|[-]?))|(?(3)[-])|(?(4)[- ]))|[- ]?)(?<Suffix>\d{4})(?:[ ]?[xX]?(?<Ext>\d{2,4}))?$

http://regexlib.com/REDetails.aspx?regexp_id=537

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Kyle Hudson
  • 898
  • 1
  • 14
  • 26
0

Those RegEx patterns are way too short.

The RegEx pattern to check whether the geographic area code is valid and the number length is correct, runs to:

2(?:0[01378]|3[0189]|4[017]|8[0-46-9]|9[012])\d{7}| 1(?:(?:1(?:3[0-48]|[46][0-4]|5[012789]|7[0-39]|8[01349])|21[0-7]|31[0-8]|[459]1\d|61[0-46-9]))\d{6}|1(?: 2(?:0[024-9]|2[3-9]|3[3-79]|4[1-689]|[58][02-9]|6[0-4789]|7[013-9]|9\d)| 3(?:0\d|[25][02-9]|3[02-579]|[468][0-46-9]|7[1235679]|9[24578])| 4(?:0[03-9]|[28][02-5789]|[37]\d|4[02-69]|5[0-8]|[69][0-79])| 5(?:0[1235-9]|2[024-9]|3[015689]|4[02-9]|5[03-9]|6\d|7[0-35-9]|8[0-468]|9[0-5789])| 6(?:0[034689]|2[0-35689]|[38][013-9]|4[1-467]|5[0-69]|6[13-9]|7[0-8]|9[0124578])| 7(?:0[0246-9]|2\d|3[023678]|4[03-9]|5[0-46-9]|6[013-9]|7[0-35-9]|8[024-9]|9[02-9])| 8(?:0[35-9]|2[1-5789]|3[02-578]|4[0-578]|5[124-9]|6[2-69]|7\d|8[02-9]|9[02569])| 9(?:0[02-589]|2[02-689]|3[1-5789]|4[2-9]|5[0-579]|6[234789]|7[0124578]|8\d|9[2-57]))\d{6}|1(?: 2(?:0(?:46[1-4]|87[2-9])|545[1-79]|76(?:2\d|3[1-8]|6[1-6])|9(?:7(?:2[0-4]|3[2-5])|8(?:2[2-8]|7[0-4789]|8[345])))| 3(?:638[2-5]|647[23]|8(?:47[04-9]|64[015789]))| 4(?:044[1-7]|20(?:2[23]|8\d)|6(?:0(?:30|5[2-57]|6[1-8]|7[2-8])|140)|8(?:052|87[123]))| 5(?:24(?:3[2-79]|6\d)|276\d|6(?:26[06-9]|686))| 6(?:06(?:4\d|7[4-79])|295[567]|35[34]\d|47(?:24|61)|59(?:5[08]|6[67]|74)|955[0-4])| 7(?:26(?:6[13-9]|7[0-7])|442\d|50(?:2[0-3]|[3-68]2|76))| 8(?:27[56]\d|37(?:5[2-5]|8[239])|84(?:3[2-58]))| 9(?:0(?:0(?:6[1-8]|85)|52\d)|3583|4(?:66[1-8]|9(?:2[01]|81))|63(?:23|3[1-4])|9561))\d{3}| 176888[234678]\d{2}|16977[23]\d{3}

The above RegEx does not deal with Guernsey, Isle of Man or Jersey numbers.

There's also a lot more needed if you need to check UK wide, mobile, freephone, NGN and Premium Rate, and other numbers too.

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
g1smd
  • 221
  • 2
  • 2
  • That's not a regex, it's a parlor trick! :D Do you actually use that monster in a production environment? – Alan Moore Feb 21 '11 at 06:20
  • It's in libphonenumber, and it rejects numbers that are too short, too long, or use an area code that does not exist. – g1smd Feb 21 '11 at 19:19
  • It's in libphonenumber, and it rejects number ranges that are too short, too long, or use an area code that does not exist. – g1smd Feb 21 '11 at 19:37
  • That's 1,394 characters. It might as well be a private key. As a regular expression it's unmaintainable and incomprehensible. If this were free spaced with thorough documentation and reference links, maybe it could be a useful answer. – aliteralmind Apr 15 '14 at 11:42