gpt4 book ai didi

html - 在 jquery onclick 按钮中使用条件

转载 作者:行者123 更新时间:2023-12-05 04:38:19 27 4
gpt4 key购买 nike

我试图确认密码强度是强密码还是弱密码,当我提交它时应该有警告消息,如“你有强密码”,当它是弱密码时,“密码无效”

这就是我现在的样子。

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) {

然后,而不是 return passwordStrength = true; 你应该做 passwordStrength = true 并添加一个 return passwordStrength 到最后您的函数,因此它将返回 false 或 true。

关于html - 在 jquery onclick 按钮中使用条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70628899/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com