terrid25
terrid25

Reputation: 1946

regex for all UK telephone numbers

I am trying to validate UK telephone numbers, which are in the format of:

01234 567890

01234567890

02012345678

020 1234 5678

I have the following regex, which works for all apart from the 020 1234 5678

^\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*$

Does anyone know why this is?

Upvotes: 3

Views: 12502

Answers (4)

Raoul
Raoul

Reputation: 3889

If you can use Perl Number::Phone::UK can check if a number is valid, allocated, mobile, personal or business... and so on.

Upvotes: 1

Jaro
Jaro

Reputation: 3887

My JS solution

var total = 0;
var passed = 0;
var failed = 0;

var r = /^(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?$/;

var pass = [
    '(0123) 456 7890',
    '012 3456 7890',
    '+44 12345 67890',
    '+44 1234567890',
    '0044 1234 567 890',
    '0044 1234 567890',
];

pass.each ( function (value) {
    var result = r.test( value );
    console.log( value, (result == true ? 'ok' : 'failed') );

    total ++;
    if ( result == true ) {
        passed ++;
    } else {
        failed ++;
    }

} );


var fail = [
    '(0123) 456 78',
    '012 3456 789',
    '+44 12345 678',
    '0044 1234 567 89',
];

fail.each ( function (value) {
    var result = r.test( value );
    console.log( value,  (result == false ? 'ok' : 'failed') );

    total ++;
    if ( result == false ) {
        passed ++;
    } else {
        failed ++;
    }
} );

setTimeout(function () {
    console.log( "PASSED " + passed + ' / ' + total );
}, 500);

Upvotes: -1

Martin Jones
Martin Jones

Reputation: 7

020 Does not follow with a 7 or 8.

The London phone code was unified into one single area code. 020. The remaining numbers are the landline, as such the format 020 7123 4567 is correct. See ofcom website.

Upvotes: -1

stema
stema

Reputation: 92976

You are missing a closing square bracket

^\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*$
                                      ^
                                     here

This should be working

^\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*$

Further you can remove some stuff from your expression.

{1} is not need every character is still matched once

[ ] is also not needed, just replace it with a space

[8,9] is wrong. it will match 8, 9 and , . Use [89] is correct.

Then it would look like that

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

If you want to allow this not matched pattern 020 1234 5678 you could do for example

^\s*\(?(020[78]?\)? ?[1-9][0-9]{2,3} ?[0-9]{4})$|^(0[1-8][0-9]{3}\)? ?[1-9][0-9]{2} ?[0-9]{3})\s*$
               ^                 ^^
made the [78] optional        allows no 2 or 3 of `[0-9]`

See it here on Regexr

I have no clue if this is a valid UK phone number!

I fixed here also another bug in the regex a missing $ before and a missing ^ after the pipe |

Upvotes: 10

Related Questions