1

How can I validate UK telephone numbers? I copied the answer from this site, but this answer only accept mobile number. I want to accept both landline and mobile number. Is it possible?

            # @reference: http://stackoverflow.com/questions/8099177/validating-uk-phone-numbers-in-php

            $telephone = "01752311149"; // not ok.
            $telephone = "07742055388"; // ok.
            $pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";

            if (!preg_match($pattern, $telephone))
            {
                $error = true;
                $message.='<error elementid="telephone" message="invalid" />';
            }

I have tried with this regex below but it doesn't work at all,

#http://stackoverflow.com/questions/14512810/regular-expression-mobile-and-landline-number
$pattern = "/^\(0\d{1,2}\)\d{3}-\d{4}$/";
hakre
  • 193,403
  • 52
  • 435
  • 836
Run
  • 54,938
  • 169
  • 450
  • 748
  • I suggest you read and understand how regular expressions work, then modify the mobile-accept number. Note that `+441`, `+442`, `01`, `02` are all valid landline numbers. – Dai Mar 15 '13 at 19:34
  • 2
    Are you sure this is a good idea? I find nothing more aggravating than a form that won't accept my phone number just because I happen to be a foreigner. As long as you don't actually call it, you can't be any more certain that the number really exists after validation - see `0207 99999999`. Plus next time London restructures its numbering plans again (as it has done three times over the past 25 years), you have to update your regex. – Pekka Mar 15 '13 at 19:34
  • 1
    I suggest you look at `libphonenumber` - it's an open-source library that Google uses in Android for validating and formatting phone numbers. – Dai Mar 15 '13 at 19:34
  • thanks for the tips. I need the validation on a browser only. no android or apple. thanks. – Run Mar 15 '13 at 19:47

2 Answers2

2

There's a selection of regular expressions for validating phone numbers at Regular Expressions for Validating and Formatting GB Telephone Numbers:

Alternatively, there's one at RegExLib.com that seems to work well:

^((\(44\))( )?|(\(\+44\))( )?|(\+44)( )?|(44)( )?)?((0)|(\(0\)))?( )?(((1[0-9]{3})|(7[1-9]{1}[0-9]{2})|(20)( )?[7-8]{1})( )?([0-9]{3}[ -]?[0-9]{3})|(2[0-9]{2}( )?[0-9]{3}[ -]?[0-9]{4}))$

Edit:

This will allow mobile, landline, and special service numbers (999, 123, etc.) -- assumes that spaces have been stripped:

'/^(?>(?>\+44|0)(?>(?!7624)(?>[12389]\d|5[56]|7[06])\d{8}|(?>(?>[58]00|1\d{2})\d{6})|(?>8001111|845464\d)|7(?>[45789]\d{8}|624\d{6}))|999|112|100|101|111|116|123|155|118\d{3}|(?>\+44|0)(?>800111|8454647))$/D'

Michael
  • 11,912
  • 6
  • 49
  • 64
  • Have you remembered to add delimeters? E.g. `/` at the start and end? – Michael Mar 15 '13 at 19:50
  • yes i did. and it accepts any number including '00000000000'. I dont want to accept '00000000000'.... – Run Mar 15 '13 at 19:56
  • Ah, sorry, I copied the basic validation. They have a more thorough one as well which I've switched into my answer. – Michael Mar 15 '13 at 19:59
  • thanks. but I think I am using this pattern. it seems to be working as I wanted - `$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)|\(?01\d{3}\)?)\s?\d{3}\s?\d{3}$/";` – Run Mar 15 '13 at 20:01
  • I know this is super old, but just in case anyone stumbles across this and tries to use it; the regex in the latest edit is invalid, the first occurrence of `(?>` seems to be wrong. Removing this seems to make it work, but I'm a total noob at regex so I might be missing something important. Also, 3 characters isn't enough to edit otherwise I'd change this myself – Sworrub Wehttam Feb 21 '16 at 17:03
1

You'd be better off using an existing library, rather than trying your own validation, as rules are somewhat complex and likely to change with time.

For example, this PHP version of Google's libphonenumber:

$phoneUtil = PhoneNumberUtil::getInstance();
try {
    $numberProto = $phoneUtil->parse('02012345678', 'GB');
} catch (NumberParseException $e) {
    echo $e;
}

Then you can check the validity of the number with:

$phoneUtil->isValidNumber($numberProto);

Furthermore, this library allows you to detect the phone number type (fixed line, mobile, voip, etc.)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 2
    This class is still being updated on github, very useful - https://github.com/giggsey/libphonenumber-for-php – rgdigi Jan 30 '15 at 16:07