Regular expression for GB based and only numeric phone number
Asked Answered
S

6

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.

Streetwalker answered 17/7, 2012 at 7:41 Comment(0)
I
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.

Infante answered 17/7, 2012 at 8:16 Comment(5)
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.Fred
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. DunhamzzzPassionate
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.Semiconscious
That being said, this is still the closest. The rest of the answers are comically wrong.Semiconscious
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}))?$Populous
L
34

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.

Lessor answered 3/8, 2012 at 8:45 Comment(5)
No, UK numbers usually have ELEVEN digits.U
@U Re-read what is said. 'UK phone numbers usually have 9 or 10 digits not including the 0'.Ladd
I could even get a single one of my test numbers to match thisSemiconscious
Have any of the upvoters even tried this with a spread of numbers? I got 2 matches out of 8 with the 1st Regex, and 0 with the 2ndSchubert
I would love to look at the information in those links, but they are now dead. Can you update them? Also have to agree with other commenters that these expressions do not work well.Proximo
I
6

A more simple version:

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

Intravenous answered 7/3, 2021 at 12:20 Comment(1)
I tried the other solutions but they appear not to work with the pattern tag in HTML. This one worked for me.Mellott
C
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

Choirboy answered 26/8, 2017 at 19:12 Comment(0)
G
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.

Gibbie answered 15/3, 2023 at 16:27 Comment(0)
M
-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);
Malm answered 16/4, 2019 at 16:2 Comment(1)
No they can have 7, 9 or 10 digits after the zero (or country code): en.wikipedia.org/wiki/…Nieman

© 2022 - 2024 — McMap. All rights reserved.