模式匹配
可能的重复:
用于电话号码验证的综合正则表达式
我正在尝试推出在 Javascript 中验证电话号码的正确模式。 num 的格式如下: 电话号码为 10 位数字 电话号码必须以 012,013,019,014 或 016 开头。 这个过程对我来说非常困惑..有人可以帮忙吗? 提前高度赞赏..
Possible Duplicate:
A comprehensive regex for phone number validation
I am trying to come out for the right pattern to validate a phone number in Javascript. The num is in the following format:
the phone num is 10 digits
the phone num must start with 012,013,019,014, or 016.
The process is very confusing for me .. can anyone help please?
Highly appreciated in advance ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试:
try:
这个模式应该有效
This pattern should work
使用如下正则表达式:
其中
phone
是您的电话号码变量。正则表达式意味着它必须以 0 开头 (^),然后是 1,然后是 2、3、4、6、9 中的任意一个,后面再加 7 个数字。Use a regex like this:
where
phone
is your phone number variable. The regex means it has to start (^) with 0, then 1, then any of 2,3,4,6,9 followed by 7 more digits.怎么样:
这是一个 正则表达式 (MDC 有一个 好页面)寻找替代(012、013、 019、014 或 016)后跟正好七位数字。开头的
^
与字符串的开头匹配,结尾的$
与结尾匹配,不允许出现无关字符。在执行此操作之前,您可能会考虑修剪空白。我使用了替代而不是
^01[23946]
只是为了清晰起见并允许轻松添加其他前缀。实时测试
旁注:我总是试图阻止人们验证电话号码。当人们需要(需要)输入您的规则不允许的内容时,它最终只会激怒人们。另外,通常10位数字的电话号码是成组书写的,例如在英国,“01234 567 890”(有时写为“01234 56 78 90”)或“020 8123 4567”(有时写为“0208 123 4567”)。在验证之前,您必须删除内部空格,以允许人们以人们所做的各种方式编写它们。
How 'bout:
That's a regular expression (MDC has a good page on them) which looks for an alternation (any of 012, 013, 019, 014, or 016) followed by exactly seven digits. The
^
at the start matches the start of the string, and the$
at the end matches the end, disallowing extraneous characters. You might consider trimming whitespace before doing this.I used an alternation rather than
^01[23946]
just for clarity and to allow for other prefixes being easily added.Live test
Side note: I always try to discourage people from validating phone numbers. It just ends up irritating people when they need (need) to input something your rules haven't allowed for. Also, usually 10-digit phone numbers are written in groups, e.g. here in the UK, "01234 567 890" (sometimes written "01234 56 78 90") or "020 8123 4567" (sometimes written "0208 123 4567"). You'd have to strip out internal spaces before validating to allow people to write them in the various ways people do.
我想你想在这里使用正则表达式。
此处的示例/教程应该会引导您走向正确的方向。
I think you want to use a regular expression here.
The example/tutorial here should steer you in the right direction.