使用正则表达式后跟模式验证密码



我们需要检查内部Web CRM的安全密码是否正确记录。

有没有办法像"$$$$Addy1234"一样验证给定的安全密码?

此密码字符串包含 4 个特殊字符、4 个字母和 4 个数字。字母部分应包含一个大写字符和一个小写字符。

我尝试的正则表达式适用于所有情况,但在字母表中,我需要至少获得一个大写字符和一个小写字符。

我尝试了以下方法,但无法获得解决方案:

$("#btn_submit").click(function () {
if ($("#txt_pasword").filter(function () {
return this.value.match(/^([-/@#!*$%^&.'_+={}()]{4})([a-zA-Z]{4})([0-9]{4})$/);
})) {
$("#txt_pasword").parent().child("span").text("pass");
} else {
$("#txt_pasword").parent().child("span").text("fail");
}
});

请提供一个想法,我应该怎么做?

提前谢谢你。

在下面的代码中,您可以使用自己的逻辑来显示错误消息,但在这里我使用了警报

$(function(){
$("#btn_submit").click(function () {
var mat_str=/^([-/@#!*$%^&.'_+={}()]{4})((?=.*[a-z])(?=.*[A-Z])(?=.*d)[a-zA-Zd]{4,})([0-9]{4})$/;
var pass=$("#txt_pasword").val();
if(pass!=null && pass!=undefined){
if(mat_str.test(pass)){
alert("pass");                
}else{
alert("fails");
}
}else{
alert("Please Enter password");
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="password" id="txt_pasword">
<span></span>
</div>
<input type="button" id="btn_submit" value="submit"/>

这对你有帮助吗?

法典:

$(document).ready(function() {
var str = "$$$$Addy1234";
var res = str.substring(0,3);
var res2 = str.substring(4,7);
var res3 = str.substring(8, 11);
var check = 0;
// check special chars
if (res.match(/([!,%,&,@,#,$,^,*,?,_,~])/))
{
check += 1;
}
// check upper/lower cases
if (res2.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))
{
check += 1;
}
// check numbers
if (res3.match(/([0-9])/))
{
check += 1;
}
if( check < 3)
{
// return false
}
if (check === 3)
{
// return true
}
});

您可以进行不同的检查:

// If password contains both lower and uppercase characters, increase strength value.
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) strength += 1
// If it has numbers and characters, increase strength value.
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) strength += 1
// If it has one special character, increase strength value.
if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1
// If it has two special characters, increase strength value.
if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1

您可以根据给定的内容检查自己的正则表达式。

使用substring您可以检查带有开始和结束的特定长度。

这会为你做吗?让我知道!:)

感谢所有为我提供此问题解决方案的人,主要是@kalaiselvan-a和@ronnie-oosting。

但是从想法来看,@kalaiselvan-a 给比大约正确,几乎没有问题,但这个可以帮助我相应地获得解决方案。

使用的正则表达式是:

/^([-/@#!*$%^&.'_+={}()]{4})((?=.*[a-z])(?=.*[A-Z])[a-zA-Z]{4})([0-9]{4})$/

第一个捕获组([-/@#!*$%^&.'_+={}()]{4})与列表中字符的 4 次完全匹配@#!*$%^&.'_+={}()(区分大小写)

第 2 个捕获组((?=.*[a-z])(?=.*[A-Z])[a-zA-Z]{4})与列表中的字符[a-z][A-Z]匹配 4 次,每个字符之一

第 3 个捕获组([0-9]{4})与列表中存在的字符精确匹配 4 倍[0-9]{4}

最新更新