[英]Javascript Regular Expression blacklist combinations for social security number validation
I have a simple social security number regex ^\\(?\\d{3}\\)?[- ]?\\d{2}[- ]?\\d{4}$
我有一个简单的社会保险号regex
^\\(?\\d{3}\\)?[- ]?\\d{2}[- ]?\\d{4}$
I'd like to blacklist some combinations of numbers for each of the three groups of numbers. 我想为三组数字中的每组数字组合添加黑名单。 Specifically I'd like to accomplish the following 3 conditions.
具体来说,我想完成以下3个条件。
if (!ssn.match(/^((000|666|9\d{2})-\d{2}-\d{4}|\d{3}-00-\d{4}|\d{3}-\d{2}-0{4})$/)) {
...
}
where ssn
is a string containing the SSN. 其中,
ssn
是包含SSN的字符串。
This regex works in 3 parts, each of which corresponds to your conditions. 此正则表达式可分为三个部分,每个部分都与您的条件相对应。
(000|666|9\\d{2})-\\d{2}-\\d{4}
matches any SSN starting with 000
, 666
, or 9XX
(000|666|9\\d{2})-\\d{2}-\\d{4}
匹配任何SSN开始000
, 666
,或9XX
\\d{3}-00-\\d{4}
matches any SSN with 00
in the middle \\d{3}-00-\\d{4}
匹配中间带有00
任何SSN \\d{3}-\\d{2}-0{4}
matches any SSN with 0000
in the end \\d{3}-\\d{2}-0{4}
匹配任何结尾为0000
SSN The ^
in the beginning and $
at the end force an exact match of ssn
. 开头的
^
和结尾的$
强制匹配ssn
。 You can omit these if you're searching as a substring. 如果要作为子字符串进行搜索,则可以忽略这些。
The (
and )
grouping operators in conjunction with |
(
和)
分组运算符结合|
allow for any of the |
允许任何
|
separated options to match. 分隔的选项进行匹配。
If a SSN doesn't match any of these three, then it passes your conditions 如果SSN与这三个都不匹配,则表明您符合条件
先前的答案很正确,但是RegEx本身可能更简单:
/^(?:000|666|9\d\d)|^\d{3}-?00|0{4}$/
我在这里找到了一个很好的正则表达式
/^(?!000)(?!666)(?!9)\\d{3}([- ]?)(?!00)\\d{2}\\1(?!0000)\\d{4}$/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.