验证英国电话号码 [英] Validate a UK phone number

查看:363
本文介绍了验证英国电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用正则表达式验证英国的电话号码在C#?

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

推荐答案

在接受答案的正则表达式不匹配所有有效的英国的电话号码,因为实在是太restricive(附加号码范围已在同时被打通,如0203 ,它认为为无效)。

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:


  • 它们可以是10或11位(有一些特殊号码的例外,但你不太可能需要验证这些信息)

  • 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)

它们由一个区域code后跟一个本地号码。面积code在三个和五个数字之间的长度变化,并且数量的本地部分占用的10或11位数字的剩余长度。对于所有的实际目的,没有任何人曾经引用只是其数量的本地部分,所以你现在可以忽略的区别,除了它是如何影响格式化。

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.

他们从零开始。

第二个数字可以是任何东西。目前没有有效的数字开始 04 06 ,但有什么能够阻止这些范围进入在以后的使用。 ( 03 最近已经投入使用)

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.

因此​​,一个基本的工作前$ P $英国电话号码pssion可能看起来像这样:

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

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

这将检查10或11位数字,从零,与格式化空格的任何数字之间,并且可选一套支架的区域code。

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.

(是的,这将允许不匹配的括号,因为我没有检查到有只有一个右括号。强制执行这将使前pression很多更复杂,我没有时间现在这种权利,反而觉得免费的,如果你想添加这个)

(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:


  • 开始数 08 09 070 是特殊的价格数字,一般不会被指定为私人号码,所以可以排除在外,如果验证的私人号码。

  • 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 数字移动(除了 070 ;见上文),因此可以排除在外,如果你'重新验证专门针对固定电话。

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

这篇关于验证英国电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆