javascript中的电话号码正则表达式验证 [英] Phone number regular expression validation in javascript

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

问题描述

你们可以在javascript中为电话号码提供正则表达式验证吗?

Can you guys please provide a regular expression validation in javascript for a phone number?

推荐答案

请通过此链接

< a href =http://james.padolsey.com/javascript/regular-expressions-in-javascript/> http://james.padolsey.com/javascript/regular-expressions-in-javascript/ [ ^ ]



希望这有帮助
Please go through this link
http://james.padolsey.com/javascript/regular-expressions-in-javascript/[^]

Hope this helps


首先你可以了解JS Regex 此处 [ ^ ]



关于正则表达式中使用的模式 - 事实证明,至少对于北美来说,有一些规范,称为 NANP



您需要准确指定您想要的内容

什么是合法分隔符?空格,破折号和句号?不允许分隔符?可以混合分隔符(例如,+ 0.111-222.3333)吗?如何处理扩展(例如,111-222-3333 x 44444)?特殊号码怎么样,比如911?区号是可选的还是必需的?



这是一个7位或10位数的正则表达式,允许扩展,分隔符是空格,破折号,或期间:



^(?:( ?: \ +??1 \ * *(?:[.-] \ S *))(?:\(\s *([2-9] 1 [02-9] |?[2-9] [2月8日] 1 | [2-9] [2月8日] [02-9])\s * \)|([2-9] 1 [02-9] | [2-9] [2月8日] 1 | [2-9] [2月8日] [02-9]))\s *(:???[.-] \s *))([2-9] 1 [02-9] | [2-9] [02-9] 1 | [2-9] [02-9] {2})\s *(:[.-] \s *)([0-9] {4})(?: \s *(? ?:#| x\。?| ext \。?|扩展名)\ * *(\ d +))?
First you can learn about JS Regex here[^]

About the pattern to use in the Regex - It turns out that there''s something of a spec for this, at least for North America, called the NANP.

You need to specify exactly what you want.
What are legal delimiters? Spaces, dashes, and periods? No delimiter allowed? Can one mix delimiters (e.g., +0.111-222.3333)? How are extensions (e.g., 111-222-3333 x 44444) going to be handled? What about special numbers, like 911? Is the area code going to be optional or required?

Here''s a regex for a 7 or 10 digit number, with extensions allowed, delimiters are spaces, dashes, or periods:

^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?






干杯,

Edo


Cheers,
Edo


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

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