Skip to content
Advertisement

Validating UK phone numbers in PHP

I purchased a contact form. Great little thing but I need to convert the validation for the phone number to allow for UK number formats – in other words, to allow for spaces.

Right now it validates without spaces and has a minimum length of 8 characters:

JavaScript

Advertisement

Answer

Phone numbers are typically horrible for regex patterns, which is what you will need.

This pattern for example:

JavaScript

That pattern will match with +44 included or not e.g.

all these will match:

JavaScript

These won’t

JavaScript

There are a load of sites that offer tutorials / cheat sheets etc. for regular expressions try some of these:

http://regexlib.com/Default.aspx

as well as a very good stack overflow post:

A comprehensive regex for phone number validation

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement