Need to modify the regex for validating the UK Phone

76 Views Asked by At

I have a php validation of the UK phone number, inputed by potential clients in my form. It looks like this:

function phoneUK(phone){
    var regex = /^(?:(?:(?:00\s?|\+)44\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})$/i;
    return regex.test(phone);
}

The problem is that in this form it only allows phone numbers with a 0 in front followed by ten digits (0XXXX XXXXXX) and since recently there are also legitimate phone numbers with eleven digits after the leading zero.

How to alter the code so these type of phone numbers are allowed?

1

There are 1 best solutions below

0
On

Try this:

function phoneUK(phone){
    var regex = /^(?:(?:(?:00\s?|\+)44\s?)|(?:\(?0))(?:\d{2}\)?\s?\d{4}\s?\d{4}|\d{2}\)?\s?\d{5}\s?\d{4}|\d{3}\)?\s?\d{3}\s?\d{3,4}|\d{3}\)?\s?\d{4}\s?\d{3,4}|\d{4}\)?\s?(?:\d{5}|\d{3}\s?\d{3}|\d{4}\s?\d{3})|\d{5}\)?\s?\d{4,5}|\d{5,6}\)?\s?\d{4,6})$/i;
    return regex.test(phone);
}

Hope this helps.