我有一个密码字段,需要在3个条件下进行验证。当我符合所有条件时,它会跳过一种强度:弱。
当我清空输入字段时,一个正则表达式仍然与条件匹配,我不知道为什么。
当您删除所有内容时,它会跳过强度较弱的内容。强度保持不变,但应该变空。有什么建议是什么错误或我可以如何改善?预先感谢
这是我的HTML:
<label class="customer__form-field js-cm-field">
<span>{{'form.password' | trans}} *</span>
<input type="password"
name="password"
required>
</label>
<div class="password-strength progress js-cm-password-strength" data-passwordstrength="empty">
<div class="progress-bar" role="progressbar">
Strength
</div>
</div>
<ul class="password-tips">
<li class="letters" data-letters="false">
<i class="fa fa-check"></i>
lower and uppercase letters
Kleine letters en hoofdletters.
</li>
<li class="numbers" data-numbers="false">
<i class="fa fa-check"></i>
letters and numbers
Letters en cijfers.
</li>
<li class="characters" data-characters="false">
<i class="fa fa-check"></i>
special characters
Speciale characters
</li>
</ul>
这是我的JS
var validation_message = "";
function returnError () {
return validation_message;
};
// Password validation
$.validator.addMethod("password", function(value) {
var strength = {
0: "empty",
1: "bad",
2: "weak",
3: "strong"
};
var score = 0;
// If password contains both lower and uppercase characters, increase strength value.
if (value.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) {
score += 1;
$('.letters').attr('data-letters', "true");
$('.js-cm-password-strength').attr('data-passwordstrength', strength[score]);
} else {
score -= 1;
$('.letters').attr('data-letters', "false");
$('.js-cm-password-strength').attr('data-passwordstrength', strength[score]);
}
// // // If it has numbers and characters, increase strength value.
if (value.match(/([a-zA-Z])/) && value.match(/([0-9])/)) {
score += 1;
$('.numbers').attr('data-numbers', "true");
$('.js-cm-password-strength').attr('data-passwordstrength', strength[score]);
} else {
score -= 1;
$('.numbers').attr('data-numbers', "false");
$('.js-cm-password-strength').attr('data-passwordstrength', strength[score]);
}
// // // If it has one special character, increase strength value.
if (value.match(/([!,%,&,@,#,$,^,*,?,_,~])/)) {
score += 1;
$('.characters').attr('data-characters', "true");
$('.js-cm-password-strength').attr('data-passwordstrength', strength[score]);
} else {
score -= 1;
$('.characters').attr('data-characters', "false");
$('.js-cm-password-strength').attr('data-passwordstrength', strength[score]);
}
if(score < 1) {
validation_message = "Het wachtwoord is te zwak kijk bij de tips om het wachtwoord sterker te maken.";
return false;
} else {
return true;
}
}, returnError);
jQuery的规则验证 http://jqueryvalidation.org/validate/
rules: {
password: {
password: true
},
}