Regular expression for GB based and only numeric phone number

Viewed 73999

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.

5 Answers

A more simple version:

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

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);
Related