Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate a UK phone number [closed]

How do I validate a UK phone number in C# using a regex?

like image 505
LeoD Avatar asked Aug 28 '08 14:08

LeoD


People also ask

How do you check if a phone number is still active or not?

Visit www.textmagic.com or download the TextMagic mobile app on google play store. Enter your phone number and country and click on Validate Number. This app will show you the status of the number if it is active or not.

How do I verify a phone number for all countries?

EPP-style phone numbers use the format + CCC . NNNNNNNNNN x EEEE , where C is the 1–3 digit country code, N is up to 14 digits, and E is the (optional) extension. The leading plus sign and the dot following the country code are required. The literal “x” character is required only if an extension is provided.

How do I validate a mobile number?

Mobile Number validation criteria:The first digit should contain numbers between 6 to 9. The rest 9 digit can contain any number between 0 to 9. The mobile number can have 11 digits also by including 0 at the starting. The mobile number can be of 12 digits also by including 91 at the starting.


1 Answers

The regex in the accepted answer does not match all valid UK numbers, as it is too restricive (additional number ranges have been opened up in the meanwhile such as 0203, which it sees as invalid).

UK phone numbers follow fairly simple rules:

  • They can be either 10 or 11 digits long (with the exception of some special numbers, but you're unlikely to need to validate those)

  • They consist of an area code followed by a local number. The area code varies in length between three and five digits, and the local portion of the number takes up the remaining length of the 10 or 11 digits. For all practical purposes, no-one ever quotes just the local portion of their number, so you can ignore the distinction now, except for how it affects formatting.

  • They start with zero.

  • The second digit can be anything. Currently no valid numbers start with 04 or 06, but there's nothing stopping these ranges coming into use in the future. (03 has recently been brought into use)

  • They can be formatted with a set of brackets and with spaces (one or more, in varying positions), but those are all entirely optional.

Therefore, a basic working expression for UK phone numbers could look like this:

/^\(?0( *\d\)?){9,10}$/

This will check for 10 or 11 digit numbers, starting with a zero, with formatting spaces between any of the digits, and optionally a set of brackets for the area code.

(and yes, this would allow mis-matched brackets, as I'm not checking that there's only one closing bracket. Enforcing this would make the expression a lot more complex, and I don't have time for this right now, but feel free to add this if you wish)

By the way, in case you want to do additional filtering, you might want to also note the following rules:

  • Numbers starting 08, 09 and 070 are special price numbers, and would not generally be given as private numbers, so can be excluded if validating a private number.

  • 07 numbers are mobile (except 070; see above) so can be excluded if you're specifically validating for a landline.

like image 176
Spudley Avatar answered Oct 20 '22 18:10

Spudley