简体   繁体   English

jQuery Validation插件的自定义规则(正则表达式)

[英]Custom rule for jquery Validation plugin (regex)

Can you help me write two rules that check phoneNumber? 您能帮我写两条检查phoneNumber的规则吗?
First rule: start with +7 or 8 then any ten numbers. 第一条规则:以+7或8开头,然后是任意十个数字。 Examples: 例子:

+77017223457 - valid
77017223457 -not valid
87017223457 - valid

Second rule: start with +7 or 8 then three numbers from set: 第二条规则:以+7或8开头,然后是集合中的三个数字:
{700, 701, 702, 705, 707, 712, 713, 717, 718,721, 725, 726, 727, 777 } then any seven numbers. {700、701、702、705、707、712、713、717、718,721、725、726、727、777},然后是任意七个数字。

Examples: 例子:

+77074446255 - valid
+77034446255 - not valid (no 703 in the set)

Thanks. 谢谢。

(?:\+7|8)(?:70[0-2]|705|707|71[23]|71[78]|721|72[5-7]|777)[0-9]+

or, less compressed but more obvious: 或者,压缩较少但更明显:

(?:\+7|8)(?:700|701|702|705|707|712|713|717|718|721|725|726|727|777)[0-9]+

To make sure that this is not a partial match, use ^ and $ 为确保这不是部分匹配,请使用^$

^(?:\+7|8)(?:700|etc|etc)[0-9]+$
var phone = $("#phoneNumber").val()
var regex = /^((\+7)|8)(700|701|702|705|707|712|713|717|718,721|725|726|727|777)[0-9]{7}$/;
if(regex.test(phone))
   // Phone is valid
else
   // Phone is invalid

更加紧凑:

/^(?/\+7|8)7(?:0[01257]|1[2378]|2[1567]|77)\d{7}$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM