C# 验证英国电话号码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:11:46  来源:igfitidea点击:

Validate a UK phone number

提问by LeoD

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

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

回答by Spudley

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

接受的答案中的正则表达式与所有有效的英国数字不匹配,因为它过于严格(同时开放了额外的数字范围,例如 0203,它认为无效)。

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 04or 06, but there's nothing stopping these ranges coming into use in the future. (03has 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.

  • 它们的长度可以是 10 位或 11 位(某些特殊数字除外,但您不太可能需要验证这些数字)

  • 它们由一个区号和一个本地号码组成。区号的长度在三到五位之间变化,号码的本地部分占据了剩余的 10 位或 11 位长度。出于所有实际目的,没有人只引用他们号码的本地部分,因此您现在可以忽略这种区别,除了它如何影响格式。

  • 他们从零开始。

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

  • 它们可以用一组括号和空格(一个或多个,在不同的位置)进行格式化,但这些都是完全可选的。

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.

这将检查 10 或 11 位数字,从零开始,在任何数字之间使用格式化空格,以及可选的一组区号括号。

(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, 09and 070are special price numbers, and would not generally be given as private numbers, so can be excluded if validating a private number.

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

  • 0809和开头的数字070是特价数字,通常不会作为私人数字给出,因此如果验证私人数字,则可以将其排除。

  • 07号码是移动的(除了070;见上文)所以如果您专门验证固定电话,可以将其排除在外。