我需要在密码中包含至少8个字符的验证,包括特殊字符和至少一个大写字母。
我试过这个脚本:
$(document).on('keyup', '.passworduser', function() {
var txtInput = $(this).val();
var regPass = "^(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[#?!@$%^&*-_]).{8,}$";
var color = "Red";
if (txtInput.length > 7) {
if (!txtInput.match(regPass)) {
$('#password_strength').html("Include a special character and at least one capital letter");
$("#password_strength").css("color", color);
} else {
$('#password_strength').html("");
}
} else {
$('#password_strength').html("Password to be a minimum of 8 characters");
$("#password_strength").css("color", color);
}
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="passworduser">
<div id="password_strength"></div>
&#13;
对某些表达方式而言,它是有效的,有些则不然。
答案 0 :(得分:1)
regPass
是字符串。它应该用反斜杠包裹以制作正则表达式或将字符串传递给RegExp
构造函数。
var regPass = /^(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[#?!@$%^&*\-_]).{8,}$/;
OR
var regPass = new RegExp("^(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[#?!@$%^&*\\-_]).{8,}$");
此外,字符类中的-
(连字符)用于选择字符范围。要按字面意思匹配,请将其转义或移动到字符类的开头或结尾。
这是包含一些更改的完整代码
// Can be moved outside
var regex = /^(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[#?!@$%^&*\-_]).{8,}$/;
$(document).on('keyup', '.passworduser', function() {
var value = $(this).val();
var color = "Red";
if (value.length > 7) {
// Use RegExp.test instead of String.match
if (regex.test(value)) {
// Method chaining
$('#password_strength').html("Include a special character and at least one capital letter")
.css("color", color);
} else {
$('#password_strength').html("");
}
} else {
$('#password_strength').html("Password to be a minimum of 8 characters")
.css("color", color);
}
});
答案 1 :(得分:0)
保持你的正则表达式简单。它更容易调试。试试这个:
function testPassword(pw) {
// for all special characters
var reSpecial = /[#?!@$%^&*\-_\\\/]/;
// for Capitals
var reCap = /[A-Z]/;
// just test both of them...
return pw.match(reSpecial) != undefined && pw.match(reCap) != undefined;
};
console.log(testPassword("asdf"));// false
console.log(testPassword("asdf5"));//false
console.log(testPassword("asdA"));//false
console.log(testPassword("asdf%"));//false
console.log(testPassword("asdfA%"));//true
$(document).on('keyup', '.passworduser', function(){
var color="Red";
if(txtInput.length > 7){
if(!testPassword(txtInput){
$('#password_strength').html("Include a special character and at least one capital letter");
$("#password_strength").css("color", color);
}else{
$('#password_strength').html("");
}
}else{
$('#password_strength').html("Password to be a minimum of 8 characters");
$("#password_strength").css("color", color);
}
});
答案 2 :(得分:0)
请尝试此操作,我已更新您的代码,以下是代码:
$(document).on('keyup', '.passworduser', function(){
var txtInput = $(this).val();
var color="Red";
var uppercase = /[A-Z]/ ;
var lowercase = /[a-z]/ ;
var number = /[0-9]/ ;
var special = /[\W]{1,}/ ;
var pswd_length = txtInput.length < 8;
if(!uppercase.test(txtInput) || !lowercase.test(txtInput) || !number.test(txtInput) || !special.test(txtInput) || pswd_length) {
$('#password_strength').html('Password must be at least 8 characters. Password must contain at least one upper case letter, one lower case letter and one digit.');
$("#password_strength").css("color", color);
//return false;
}
else {
$('#password_strength').html("");
}
});
我希望,这可能对你有所帮助。