密码表验证 - 正则表达式
Password Meter Validation - RegEx
密码表指导用户开发强密码。我想在电表上实施以下密码要求。我在正则表达式中添加了下面突出显示的代码,但是该代码未检测到所需的密码准则。检测以下要求的正确代码是什么?
- 8+ 个字符
- 大小写字母
- 特殊字符
- 不得包含 4 个以上的连续字母
- 不得包含 4 个以上的连续数字
示例:
测试%401 = 不足
2323Ejsdh! = 不足
Tlv!897% = 强
302^PLs# = 强
if(val.length > 7 && val.match(/\d{4}/) && val.match(/[a-zA-Z]{4}/) && val.match(/[~,!,@,#,$,%,^,&,*,(,),+,-,?]/))
这是适合您任务的单一正则表达式方法:
^(?=.*[a-zA-Z])(?=.*[^\w\s])(?!.*(?:[a-zA-Z]{4}|\d{4})).{8,}$
如果您需要匹配特定的 "special" 个字符,请使用您的 [~!@#$%^&*()+?-]
class 而不是 [^\w\s]
(不是单词或空白字符).
细分:
var rx = RegExp ("^" + // Start of string
"(?=.*[a-zA-Z])" + // Letter is required
"(?=.*[^\w\s])" + // A special character is required
"(?!.*(?:[a-zA-Z]{4}|\d{4}))" + // No 4 letters or digits on end are allowed
".{8,}" + // The whole length can be 8 or more characters
"$" // End of string
);
这是一个工作片段:
var ins = "Test%401";
var ins1 = "2323Ejsdh!";
var strong1 = "Tlv!897%";
var strong2 = "302^PLs#";
var rx = RegExp ("^" + // Start of string
"(?=.*[a-zA-Z])" + // Letter is required
"(?=.*[^\w\s])" + // A special character is required
"(?!.*(?:[a-zA-Z]{4}|\d{4}))" + // No 4 letters or digits on end are allowed
".{8,}" + // The whole length can be 8 or more characters
"$" // End of string
);
document.getElementById("r").innerHTML += rx.test(ins) + "<br/>";
document.getElementById("r").innerHTML += rx.test(ins1) + "<br/>";
document.getElementById("r").innerHTML += rx.test(strong1) + "<br/>";
document.getElementById("r").innerHTML += rx.test(strong2) + "<br/>";
<div id="r"/>
另一种方式是拆分条件:
function CheckPass(val) {
if(val.length <= 7) {
return "Password is too short!";
}
else if (/\d{4}/.test(val)) {
return "Password contains 4 digits on end!";
}
else if (/[a-zA-Z]{4}/.test(val)) {
return "Password contains 4 letters on end!";
}
else if (!/[~!@#$%^&*()+?-]/.test(val)) {
return "Password must contain at least one special character from the \"~!@#$%^&*()+?-\" set!";
}
else if (!/[a-z]/i.test(val)) {
return "Password must contain at least one letter!";
}
else
return "Password is strong!";
}
document.getElementById("r").innerHTML += "<b>Test%401</b>: "+CheckPass("Test%401") + "<br/>";
document.getElementById("r").innerHTML += "<b>2323Ejsdh!</b>: "+CheckPass("2323Ejsdh!") + "<br/>";
document.getElementById("r").innerHTML += "<b>23!34%12!</b>: "+CheckPass("23!34%12!") + "<br/>";
document.getElementById("r").innerHTML += "<b>23w34W12D</b>: "+CheckPass("23w34W12D") + "<br/>";
document.getElementById("r").innerHTML += "<b>Tlv</b>: "+ CheckPass("Tlv") + "<br/>";
document.getElementById("r").innerHTML += "<b>Tlv!897%</b>: "+CheckPass("Tlv!897%") + "<br/>";
document.getElementById("r").innerHTML += "<b>302^PLs#</b>: "+ CheckPass("302^PLs#");
<div id="r"/>
密码表指导用户开发强密码。我想在电表上实施以下密码要求。我在正则表达式中添加了下面突出显示的代码,但是该代码未检测到所需的密码准则。检测以下要求的正确代码是什么?
- 8+ 个字符
- 大小写字母
- 特殊字符
- 不得包含 4 个以上的连续字母
- 不得包含 4 个以上的连续数字
示例:
测试%401 = 不足
2323Ejsdh! = 不足
Tlv!897% = 强
302^PLs# = 强
if(val.length > 7 && val.match(/\d{4}/) && val.match(/[a-zA-Z]{4}/) && val.match(/[~,!,@,#,$,%,^,&,*,(,),+,-,?]/))
这是适合您任务的单一正则表达式方法:
^(?=.*[a-zA-Z])(?=.*[^\w\s])(?!.*(?:[a-zA-Z]{4}|\d{4})).{8,}$
如果您需要匹配特定的 "special" 个字符,请使用您的 [~!@#$%^&*()+?-]
class 而不是 [^\w\s]
(不是单词或空白字符).
细分:
var rx = RegExp ("^" + // Start of string
"(?=.*[a-zA-Z])" + // Letter is required
"(?=.*[^\w\s])" + // A special character is required
"(?!.*(?:[a-zA-Z]{4}|\d{4}))" + // No 4 letters or digits on end are allowed
".{8,}" + // The whole length can be 8 or more characters
"$" // End of string
);
这是一个工作片段:
var ins = "Test%401";
var ins1 = "2323Ejsdh!";
var strong1 = "Tlv!897%";
var strong2 = "302^PLs#";
var rx = RegExp ("^" + // Start of string
"(?=.*[a-zA-Z])" + // Letter is required
"(?=.*[^\w\s])" + // A special character is required
"(?!.*(?:[a-zA-Z]{4}|\d{4}))" + // No 4 letters or digits on end are allowed
".{8,}" + // The whole length can be 8 or more characters
"$" // End of string
);
document.getElementById("r").innerHTML += rx.test(ins) + "<br/>";
document.getElementById("r").innerHTML += rx.test(ins1) + "<br/>";
document.getElementById("r").innerHTML += rx.test(strong1) + "<br/>";
document.getElementById("r").innerHTML += rx.test(strong2) + "<br/>";
<div id="r"/>
另一种方式是拆分条件:
function CheckPass(val) {
if(val.length <= 7) {
return "Password is too short!";
}
else if (/\d{4}/.test(val)) {
return "Password contains 4 digits on end!";
}
else if (/[a-zA-Z]{4}/.test(val)) {
return "Password contains 4 letters on end!";
}
else if (!/[~!@#$%^&*()+?-]/.test(val)) {
return "Password must contain at least one special character from the \"~!@#$%^&*()+?-\" set!";
}
else if (!/[a-z]/i.test(val)) {
return "Password must contain at least one letter!";
}
else
return "Password is strong!";
}
document.getElementById("r").innerHTML += "<b>Test%401</b>: "+CheckPass("Test%401") + "<br/>";
document.getElementById("r").innerHTML += "<b>2323Ejsdh!</b>: "+CheckPass("2323Ejsdh!") + "<br/>";
document.getElementById("r").innerHTML += "<b>23!34%12!</b>: "+CheckPass("23!34%12!") + "<br/>";
document.getElementById("r").innerHTML += "<b>23w34W12D</b>: "+CheckPass("23w34W12D") + "<br/>";
document.getElementById("r").innerHTML += "<b>Tlv</b>: "+ CheckPass("Tlv") + "<br/>";
document.getElementById("r").innerHTML += "<b>Tlv!897%</b>: "+CheckPass("Tlv!897%") + "<br/>";
document.getElementById("r").innerHTML += "<b>302^PLs#</b>: "+ CheckPass("302^PLs#");
<div id="r"/>