Validate UK phone number including its area code

♀尐吖头ヾ 提交于 2019-12-14 03:55:08

问题


I have written regex (combining available from diff threads) to validate the UK numbers.

As per regex it should validate the number 0208 123-4567. But it does not. What is the issue?

Example : http://ideone.com/pm1GT2

<?php
$number = '0208 123-4567';
validateUsNumber($number);
function validateUsNumber($number)
{
    $number= preg_replace( '/[^0-9]/', '', $number);
    $pattern = '/^(\+44\s?7\d{3}|\(?07\d{3}\)|\(?02\d{3}\)|\(?01\d{3}\)?)\s?\d{3}\s?\d{3}$/';
    $match = preg_match($pattern,$number);  
    if ($match != false)
    {
    echo " We have a valid UK phone number";
    }
    else
    {
    echo " We have an invalid UK phone number " ;
    }
}

回答1:


You can try to use this regexp instead:

$pattern = '^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$^';

So your code should look like this:

<?php
$number = '0208 123-4567';
$number = '+44 20 8985 5577'; // or this
validateUsNumber($number);
function validateUsNumber($number)
{
    $pattern = '^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$^';
    $match = preg_match($pattern,$number);  
    if ($match != false)
    {
    echo " We have a valid UK phone number";
    }
    else
    {
    echo " We have an invalid UK phone number " ;
    }
}

This is taken from http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers and you can find more GB-specific telephone number validations there. I used "1.1 Match GB telephone number in any format".




回答2:


it will match this :

02083) 123456

and not this :

0208 123-4567

because of a few reasons

  • you have the second bracket compulsary, change \(?02\d{3}\) to \(?02\d{3}\)?
  • you have kept {3} and trying to match with 2 digits, change \(?02\d{3}\)? to \(?02\d{2}\)?
  • you have not included - hiphen anywhere, so obviously it wont match. change the last part to \s?\d{3}-\s?\d{3}$.
  • your example has 4 digits after hiphen ( -4567 ), so change the last part of your regex for quantifier {4}.

Thus your final regex should look like :

^(\+44\s?7\d{3}|\(?07\d{3}\)|\(?02\d{3}\)?|\(?01\d{3}\)?)\s?\d{3}[-\s]?\d{4}$

this regex will match your need 0208 123-4567

as a suggestion, remove the + from the regex check demo here : http://regex101.com/r/kM8bC7




回答3:


Your pattern is basically looking for a nicely formatted number, but its being tested against the result of stripping non-numeric characters. You'll never have leading +44, white space or parenthesis after this:

$number= preg_replace( '/[^0-9]/', '', $number);


来源:https://stackoverflow.com/questions/23195191/validate-uk-phone-number-including-its-area-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!