VB.net 中英国电话格式的 ASP 正则表达式
我想要 VB.net 中的电话字段的正则表达式验证器。请参阅下面的要求:
电话格式应为 (+)xx-(0)xxxx-xxxxxx ext xxxx (可选)示例我的号码将显示为 44-7966-591739 屏幕格式将显示为 +44-(0)7966 -591739 分机
请建议。
此致, 尤夫
I want regular expression validator for my telephone field in VB.net. Please see the requirement below:
Telephone format should be (+)xx-(0)xxxx-xxxxxx ext xxxx (Optional) example my number would appear as 44-7966-591739 Screen would be formatted to show +44-(0)7966-591739 ext
Please suggest.
Best Regards,
Yuv
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(0) 在电话号码显示中无效。将其删除。
它是 +44 7966 591739 或 07966 591739。
RegEx 模式在很多方面都是低效的:
上面简化为:
还有更大的问题:
已经找到了前导 +44 或前导 0 一次,为什么还要一次又一次地搜索它?
简化为
但是,上述模式仅适用于 UK 4+6、3+7 和 2+8 格式号码,不适用于 3+6、4+5、5+5 和 5+4 格式号码。
模式不够完善。
电话号码验证和格式化需要分为单独的步骤。允许多种输入格式,提取重要数字并丢弃各种拨号前缀,然后将剩余号码严格格式化为国际或国家格式。
对于伦敦号码,带空格的正确格式是:
+44 20 3555 7890 或 020 3555 7890 或 (020) 3555 7890
并且没有空格:
+442035557890 或 02035557890。
括号中的 (0) 永远无效。不要使用它。
英国电话号码使用多种格式:2+8、3+7、3+6、4+6、4+5、5+5、5+4。一些用户不知道哪种格式适合哪个数字范围,并且可能在输入时使用错误的格式。让他们这样做吧;你对数字感兴趣。
第 1 步:检查输入格式是否有效
确保输入内容类似于英国电话号码。接受各种拨号前缀,+44、011 44、00 44,带或不带括号、连字符或空格;或带有前导 0 的国家格式。让用户对数字的其余部分使用他们想要的任何格式:(020) 3555 7788 或 00 (44) 203 555 7788 或 02035-557-788,即使它的格式错误那个特定的数字。不用担心括号不平衡。输入的重要部分是确保其位数正确。标点符号和空格并不重要。
上述模式匹配可选的左括号,后跟 00 或 011 和可选的右括号,后跟可选的空格或连字符,最后是可选的左括号。或者,最开始的左括号后跟一个文字+,后面不带空格或连字符。前两个选项中的任何一个后跟 44 和可选的右括号,后跟可选的空格或连字符,后跟可选括号中的可选 0,后跟可选的空格或连字符,后跟可选的左括号(国际格式)。或者,该模式匹配可选的初始左括号,后跟 0 干线代码(国家格式)。
前一部分后面是 NDC(区号)和用户电话号码,格式为 2+8、3+7、3+6、4+6、4+5、5+5 或 5+4,带或不带空格和/或连字符。这还包括在用户认为区号结束和本地用户号码开始之后提供可选的右括号和/或可选的空格或连字符。该模式允许任何格式与任何 GB 数一起使用。如果用户在输入时使用了错误的数字格式,则必须通过后续逻辑更正显示格式。
该模式以可选的分机号结尾,该分机号排列为可选的空格或连字符,后跟 x、ext 和可选的句点或 #,后跟分机号数字。整个模式不需要检查平衡括号,因为这些括号将在下一步中从数字中删除。
此时,您并不关心该数字是否以 01 或 07 或其他形式开头。您并不关心它是否是有效的区号。后续步骤将处理这些问题。
第 2 步:提取 NSN,以便可以更详细地检查长度和范围
使用上述模式检查输入看起来像 GB 电话号码后,下一步是提取 NSN 部分,以便可以更详细地检查其有效性,然后针对适用的数字范围以正确的方式进行格式化。
使用上面的模式从 $1 中提取“44”即可知道使用了国际格式,否则如果 $1 为空,则假定为国家格式。
从 $3 中提取可选分机号码详细信息并存储以供以后使用。
从 $2 中提取 NSN(包括空格、连字符和括号)。
第 3 步:验证 NSN
从 $2 中删除空格、连字符和括号,并使用进一步的正则表达式模式来检查长度和范围并识别数字类型。
这些模式将更加简单,因为它们不必处理各种拨号前缀或国家/地区代码。
因此,匹配有效手机号码的模式就像
高级费率一样简单。
每种号码类型都有许多其他模式:固定电话、商业费率、非地理、VoIP 等。
通过将问题分解为几个步骤,可以允许非常广泛的输入格式,并且对 NSN 的数字范围和长度进行非常详细的检查。
第 4 步:存储号码
提取并验证 NSN 后,将号码与国家/地区代码以及所有其他数字(不含空格或标点符号)一起存储,例如 442035557788。
第 5 步:格式化用于显示的数字
可以使用另一组简单规则来格式化数字,并在开头添加必需的+44或0。
以03 开头的数字的规则
格式为
,以 02 开头的数字的
规则格式为
完整列表很长。我可以将其全部复制并粘贴到该线程中,但随着时间的推移,很难在多个位置维护该信息。目前,完整列表可在以下网址找到:http://aa-asterisk.org。 uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers
The (0) is not valid in phone number display. Remove it.
It's +44 7966 591739 or 07966 591739.
The RegEx pattern is inefficient in multiple ways:
The above simplifies to:
There are bigger problems:
Having found the leading +44 or leading 0 once, why keep on searching for it again and again?
simplifies to
However, the above pattern caters only for UK 4+6, 3+7 and 2+8 format numbers and not for 3+6, 4+5, 5+5 and 5+4 format numbers.
The pattern is inadequate.
Phone number validation and formatting needs to be broken down into separate steps. Allow a wide range of input formats, extract the vital digits and throw away the various dial prefixes, then strictly format the remaining number in international or national format.
For London numbers, the correct format with spaces is:
+44 20 3555 7890 or 020 3555 7890 or (020) 3555 7890
and without spaces:
+442035557890 or 02035557890.
(0) in parentheses is NEVER valid. Do not use it.
UK phone numbers use a variety of formats: 2+8, 3+7, 3+6, 4+6, 4+5, 5+5, 5+4. Some users don't know which format goes with which number range and might use the wrong one on input. Let them do that; you're interested in the DIGITS.
Step 1: Check the input format looks valid
Make sure that the input looks like a UK phone number. Accept various dial prefixes, +44, 011 44, 00 44 with or without parentheses, hyphens or spaces; or national format with a leading 0. Let the user use any format they want for the remainder of the number: (020) 3555 7788 or 00 (44) 203 555 7788 or 02035-557-788 even if it is the wrong format for that particular number. Don't worry about unbalanced parentheses. The important part of the input is making sure it's the correct number of digits. Punctuation and spaces don't matter.
The above pattern matches optional opening parentheses, followed by 00 or 011 and optional closing parentheses, followed by an optional space or hyphen, followed by optional opening parentheses. Alternatively, the initial opening parentheses are followed by a literal + without a following space or hyphen. Any of the previous two options are then followed by 44 with optional closing parentheses, followed by optional space or hyphen, followed by optional 0 in optional parentheses, followed by optional space or hyphen, followed by optional opening parentheses (international format). Alternatively, the pattern matches optional initial opening parentheses followed by the 0 trunk code (national format).
The previous part is then followed by the NDC (area code) and the subscriber phone number in 2+8, 3+7, 3+6, 4+6, 4+5, 5+5 or 5+4 format with or without spaces and/or hyphens. This also includes provision for optional closing parentheses and/or optional space or hyphen after where the user thinks the area code ends and the local subscriber number begins. The pattern allows any format to be used with any GB number. The display format must be corrected by later logic if the wrong format for this number has been used by the user on input.
The pattern ends with an optional extension number arranged as an optional space or hyphen followed by x, ext and optional period, or #, followed by the extension number digits. The entire pattern does not bother to check for balanced parentheses as these will be removed from the number in the next step.
At this point you don't care whether the number begins 01 or 07 or something else. You don't care whether it's a valid area code. Later steps will deal with those issues.
Step 2: Extract the NSN so it can be checked in more detail for length and range
After checking the input looks like a GB telephone number using the pattern above, the next step is to extract the NSN part so that it can be checked in greater detail for validity and then formatted in the right way for the applicable number range.
Use the above pattern to extract the '44' from $1 to know that international format was used, otherwise assume national format if $1 is null.
Extract the optional extension number details from $3 and store them for later use.
Extract the NSN (including spaces, hyphens and parentheses) from $2.
Step 3: Validate the NSN
Remove the spaces, hyphens and parentheses from $2 and use further RegEx patterns to check the length and range and identify the number type.
These patterns will be much simpler, since they will not have to deal with various dial prefixes or country codes.
The pattern to match valid mobile numbers is therefore as simple as
Premium rate is
There will be a number of other patterns for each number type: landlines, business rate, non-geographic, VoIP, etc.
By breaking the problem into several steps, a very wide range of input formats can be allowed, and the number range and length for the NSN checked in very great detail.
Step 4: Store the number
Once the NSN has been extracted and validated, store the number with country code and all the other digits with no spaces or punctuation, e.g. 442035557788.
Step 5: Format the number for display
Another set of simple rules can be used to format the number with the requisite +44 or 0 added at the beginning.
The rule for numbers beginning 03 is
formatted as
and for numbers beginning 02 is
formatted as
The full list is quite long. I could copy and paste it all into this thread, but it would be hard to maintain that information in multiple places over time. For the present the complete list can be found at: http://aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers
用于验证:
正如 bobince 指出的那样,您应该灵活地使用电话号码,因为输入电话号码的方法有很多种。
验证该值的一种简单而有效的方法是首先删除所有非数字值,然后确保它的长度至少为 11 位,并且 - 如果您限制为英国数字 - 然后检查它以 0 或 44 开头。
我懒得去查找 vb.net 语法,但大致如下:(
\D
是任何非 0-9 的正则表达式。)到 格式 请求的号码,假设您有一个相对固定的输入想要显示到页面上,类似这样的事情可能会起作用:
replace:
with:
这是相当灵活的,但不会接受任何旧的电话号码。目前它一开始就需要一个国际代码,我不太确定它们的规则是否能完美运行,但它可能足以满足您的需要。
在正则表达式注释模式下对该正则表达式的解释(因此如果需要它可以直接用作正则表达式):
For validation:
As bobince points out, you should be flexible with phone numbers because there are so many ways to enter them.
One simple yet effective way to validate the value is first strip all non-numeric values, then make sure it is at least 11 digits long, and - if you're limiting to UK numbers - then check it starts with either 0 or 44.
I can't be bothered looking up vb.net syntax, but something along the lines of this:
(The
\D
is regex for anything not 0-9.)To format a number as requested, assuming you've got a relatively fixed input that you want to display to a page, something like this might work:
replace:
with:
That's fairly flexible but wont accept any old phone number. It currently required an international code at the start, and I'm not quite sure on the rules of them to know if it's going to work perfectly, but it might be good enough for what you need.
An explanation of that regex, in regex comment mode (so it can be used directly as a regex if necessary):
在国际格式中,切勿在括号中包含 (0)。
ITU E.123 对此提出警告:http://www .itu.int/rec/T-REC-E.123-200102-I/en
正如: http:// revk.www.me.uk/2009/09/it-is-not-44-0207-123-4567.html
Never include a (0) in parentheses in the international format.
ITU E.123 warns against it: http://www.itu.int/rec/T-REC-E.123-200102-I/en
as does: http://revk.www.me.uk/2009/09/it-is-not-44-0207-123-4567.html