如何验证电话号码和 - [英] How to Validation phone number and -

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

问题描述

对于电话号码的验证并包含 -

For the validation for the phone number and included -

@^ \ +(?:[0-9]?){6,14} [ 0-9] $

@"^\+(?:[0-9] ?){6,14}[0-9]$"

我必须验证电话号码 -

I have to validate phone number with -

示例333-333-3333 (哪个是有效号码)。

example 333-333-3333 (Which is valid number).

如何自定义有效的正则表达式 - 电话号码

how to customize the valid Regex with - into phone number

推荐答案

这取决于你想要的严格程度,看起来这个正则表达式并不是特别严格。
这个正则表达式说:

well it depends on how strict you want to be it doesn't seem like this regex is especially strict. this regex says:


  1. 从行首开始

  2. 匹配一个 + (或者可能是1或0)似乎
    不明确(但可能不依赖于实现),因为
    捕获括号: ()分解 \ + 的关系?

  3. 可能放错地方

  4. 匹配任何数字 0-9 1或0次6-14次

  5. 然后一位 0-9

  6. 然后结束。

  1. start at beginning of line
  2. match one + (or maybe 1 or 0) which seems ambiguous (but may not be depending on implementation) because the capture parentheses:() breaks up the relationship of the \+ and the ?
  3. possibly misplaced :
  4. match any digit 0-9 1 or 0 times 6-14 times
  5. then one digit 0-9
  6. then end of line.

你还注意到任何反斜杠都必须加倍...... @\\b用于单词边界。
您可能想尝试类似......

also you note that any backslash will have to be doubled... @"\\b" for a word boundary. you may want to try something like...

@"\\b[\\d]{3}\\-[\\d]{3}\\-[\\d]{4}\\b"
would I think match your example, but it wouldn't match
(555) 555 - 5555 or
555.555.5555 or
+44 1865  55555

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

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