我试图确认密码强度是强还是弱,密码是强的,当我提交时,它应该有类似";您拥有强大的密码";并且当其弱";无效密码";
这就是我现在的样子。
function checkPasswordStrength() {
var passwordStrength = false;
var number = /([0-9])/;
var alphabets = /([a-zA-Z])/;
var special_characters = /([~,!,@,#,$,%,^,&,*,-,_,+,=,?,>,<])/;
if ($('#password').val().length < 8) {
$('#password-strength-status').removeClass();
$('#password-strength-status').addClass('weak-password');
$('#password-strength-status').html("Weak (should be atleast 8 characters.)");
} else {
if ($('#password').val().match(number) && $('#password').val().match(alphabets) && $('#password').val().match(special_characters)) {
$('#password-strength-status').removeClass();
$('#password-strength-status').addClass('strong-password');
$('#password-strength-status').html("Strong");
return passwordStrength = true;
} else {
$('#password-strength-status').removeClass();
$('#password-strength-status').addClass('medium-password');
$('#password-strength-status').html("Medium (should include alphabets, numbers and special characters.)");
}
}
}
$('#btn-submit').click(function () {
if (passwordStrength == false) {
alert("INVALID PASSWORD");
} else {
alert("You have Strong PASSWORD");
}
</script>
这只是为了教育目的,我刚刚开始jquery。。提前谢谢你。。
您需要调用函数,而不仅仅是检查变量。也是如此
$('#btn-submit').click(function () {
if (checkPasswordStrength() === false) {
而不是
$('#btn-submit').click(function () {
if (passwordStrength == false) {
然后,您应该只执行passwordStrength = true
,而不是return passwordStrength = true;
,并在函数的最后添加一个return passwordStrength
,这样它就会返回false或true。
看起来变量范围不正确。var passwordStrength
应该放在checkPasswordStrength
函数之外。
var passwordStrength
function checkPasswordStrength() {
....