James Oliver
James Oliver

Reputation: 23

UK Phone Number Regex - no spaces or brackets

I have only started working with Regex today and need to validate UK telephone numbers.

I don't want any space or brackets to pass validation but I do want the following to be valid:

Numbers starting with:

Then the numbers thereafter to be between 9 and 10 digits in length, with the first of these digits being either: 1,2,3,7,8

I have written this:

^((^\+44)|^(4{2})|(^0{2}4{2})|(^0)|)[12378]\d{8,9}$

See: https://regex101.com/r/jY8aK4/1

It seemed to work but then I realised that 112222222 and 111111111 passed which they shouldn't and I can't think how to fix it.

Upvotes: 2

Views: 1493

Answers (3)

Pruthvi Raj
Pruthvi Raj

Reputation: 3036

Try using the following regex:

^(0044|0|\+?44)[12378]\d{8,9}$

DEMO

Upvotes: 3

Srini
Srini

Reputation: 1636

If you can use Perl, Number Phone UK has every convenience method you could possibly need to validate UK phone numbers.

Upvotes: 1

Stephen Gildea
Stephen Gildea

Reputation: 385

You regex is almost correct. The problem is the | at the end of your first group. It allows a null prefix. See the warning in the explanation at the URL you posted.

Upvotes: 1

Related Questions