20

I want a regular expression code which allows only numeric phone number (e.g 1234567890) as well as GB format number (e.g 123-456-7890).

This expression must have to work for both conditions.

Currently I am using below regular expression which only allows GB phone number

/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/ 

I need help to allow only numeric number also.

AStopher
  • 4,207
  • 11
  • 50
  • 75
sandeepsure
  • 1,113
  • 1
  • 10
  • 17

6 Answers6

46

There are some UK phone number regular expressions here.

The most highly rated one from that page is:

^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$

It is described as:

Matches     +447222555555   | +44 7222 555 555 | (0722) 5555555 #2222
Non-Matches (+447222)555555 | +44(7222)555555  | (0722) 5555555 #22

Personally I would not put a strict regex on a phone number unless it is absolutely required to have the users actual phone number for identity verification or something involving payments. There are lots of variations of phone numbers, some users could even use Skype as their primary phone, and by putting a validation on you're just going to block/frustrate users.

Also note you have got the UK format wrong, UK phone numbers should contain 11 digits and are normally in the format (01234) 123123 - we never use dashes and first number should always be a 0.

James Sims
  • 116
  • 8
Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
  • 11
    I know this is a few years old, but just to add that be aware that in the UK there are also some 10 digit numbers. My old company had one and it was a royal pain as 99% of sites that validate validates for 11. – Fetchez la vache Jun 03 '15 at 10:39
  • Hey, just wanted comment one thing. Actually, this regular expression accepts "00000000000". How can this be possible?. I don't think that UK does have this kind of number. Replying to this post would be highly recommended. Dunhamzzz – Visal Varghese Jun 07 '17 at 07:23
  • 1
    Lots of numbers for crisis helplines and the like won't get caught by this. For example childline is `0800 1111`, samaritans is `116 123` and there are many more. Also it seems to not match the end of the line properly, when testing in regex101 with `gim` flags, it counted the 11 digits from one line running onto another. – Matt Fletcher Jan 21 '18 at 12:16
  • That being said, this is still the closest. The rest of the answers are comically wrong. – Matt Fletcher Jan 21 '18 at 12:21
  • I've found a list of all possible UK landline and mobile formats at https://www.area-codes.org.uk/formatting.php (it's accuracy hasn't been checked but it "looks legit") you'll see that any assumption you make like "11 digits" or "10 digits" is way off.... – Andy Preston Sep 20 '20 at 10:18
  • If you ever get a `Uncaught SyntaxError: Invalid regular expression - Invalid escape` remove the two slashes before the very last hashtag (#). This worked for me: `^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?#(\d{4}|\d{3}))?$` – Maksim Dimitrov Aug 30 '21 at 17:46
35

UK phone numbers should contain 11 digits

UK phone numbers usually have 9 or 10 digits not including the 0 trunk code or +44 country code. A few have only 7 digits.

and are normally in the format (01234) 123123

The example shows a UK 4+6 format number. The UK uses a variety of formats including 2+8, 3+7, 4+6, 4+5, 5+5 and 5+4 for geographic numbers and 0+10, 0+9 and 0+7 for non-geographic numbers.

There's a handy list at:

http://www.aa-asterisk.org.uk/index.php/Number_format

Match UK telephone number in any format

^(?:(?:\(?(?: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})?$ 

The above pattern allows the user to enter the number in any format they are comfortable with. Don't constrain the user into entering specific formats.

Extract NSN, prefix and extension

^(\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)(44)\)?[\s-]?)?\(?0?(?:\)[\s-]?)?([1-9]\d{1,4}\)?[\d[\s-]]+)((?:x|ext\.?|\#)\d{3,4})?$

$2 will be '44' if international format was used, otherwise assume national format.

$4 contains the extension number if present.

$3 contains the NSN part.

Validation and formatting

Use further RegEx patterns to check the NSN has the right number of digits for this number range. Finally, store the number in E.164 format or display it in E.123 format.

There's a very detailed list of validation and display formatting RegEx patterns for UK numbers at:

Regular Expressions for Validating and Formatting GB Telephone Numbers

It's too long to reproduce here and it would be difficult to maintain multiple copies of this document.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
g1smd
  • 395
  • 2
  • 2
6

A more simple version:

^((\+44)|(0)) ?\d{4} ?\d{6}$

user15347693
  • 61
  • 1
  • 1
  • I tried the other solutions but they appear not to work with the pattern tag in HTML. This one worked for me. – wandesky Jan 10 '22 at 08:54
0

It is kind of complex to test a UK phone number since the format is quite different for different provinces. Check out this link to know for yourself how many formats can a UK phone number have.

Use

^((((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\(\d{4}|\d{3}))?)|((\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3})|((((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\(\d{4}|\d{3}))?$

Created using information from regexlib.com

Values that match the regex:

01222 555 555 | (010) 55555555 #2222 | 0122 555 5555#222 | 07222 555555 | (07222) 555555 | +44 7222 555 555 | +447222555555 | +44 7222 555 555 | (0722) 5555555 #2222

Values that do not match the regex:

01222 555 5555 | (010) 55555555 #22 | 0122 5555 5555#222 | 7222 555555 | +44 07222 555555 | (+447222) 555555 | (+447222)555555 | +44(7222)555555 | (0722) 5555555 #22

Revanth Kumar
  • 809
  • 12
  • 18
0

After spending some time researching UK Phone regular expressions, I got this:

/^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+441\s?\d{3}|\(?01\d{3}\)?)\s?\d{5})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/

Similar to other answers but slightly edited. I've added an options for 10 digit numbers (allowing numbers starting with 01/+441 as they only start with those) and it doesn't affect the 11 digit numbers.

Mahdi
  • 1
  • 1
-3

Instead of all these above, notice that UK numbers always have 10 numbers after +44 or 0 or 0044.(Correct me if I am wrong)

To avoid these massive regex you could use this regex

^(\+44\s?\d{10}|0044\s?\d{10}|0\s?\d{10})?$

Along with a space trimmer. eg in PHP

trimmedPhoneNumber= preg_replace('/\s+/', '', $phoneNumber);
  • 5
    No they can have 7, 9 or 10 digits after the zero (or country code): https://en.wikipedia.org/wiki/Telephone_numbers_in_the_United_Kingdom#Format – Ben Collins Dec 10 '19 at 10:07