gpt4 book ai didi

javascript - 提交表单时不显示错误类

转载 作者:行者123 更新时间:2023-12-01 02:22:39 25 4
gpt4 key购买 nike

我正在尝试使用 JavaScript 验证我的注册表单。我提交了表单,默认操作被阻止,但我的错误处理程序类都没有显示,我的错误日志中也没有收到任何错误。如果有人能告诉我我做错了什么,我将不胜感激。如果用户未填写输入,我会尝试在输入字段上显示红色背景。

$(document).ready(function () {
$("#signupForm").submit(function (e) {
removeFeedback();
var errors = validateSignup();
if (errors == "") {
return true;
} else {
provideFeedback(errors);
e.preventDefault();
return false
}
});

function validateSignup() {
var errorFields = new Array();
//Check required fields to see if they have anything in them
if ($('#signupFirst').val() == "") {
errorFields.push('first');
}
if ($('#signupLast').val() == "") {
errorFields.push('last');
}

if ($('#signupEmail').val() == "") {
errorFields.push('email');
}

if ($('#signupPassword').val() == "") {
errorFields.push('pwd');
}

if (!($('#signupEmail').val().indexOf(".") > 2) && ($('#signupEmail').val().indexOf("@"))) {
errorFields.push('email');
}
return errorFields();
}


function provideFeedback(errorFields) {
for (var i = 0; i < errorFields.length; i++) {
$("#" + errorFields[i]).addClass("inputError");
$("#" + errorFields[i] + "Error").removeClass("errorFeedback");
}
}


function removeFeedBack() {
$('input').each(function () {
$(this).removeClass("inputError");
});
}

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="index-bg-wrapper">
<div class="main-signup-container">
<form id="signupForm" class="signup-form" action='include/signup.inc.php' method='POST'>
<input id="signupFirst" type="text" name="first" placeholder="First Name">
<input id="signupLast" type="text" name="last" placeholder="Last Name">
<input id="signupEmail" type="text" name="email" placeholder="Email">
<input id="signupPassword" type="password" name="pwd" placeholder="Password">
<button type="submit" name="submit">Signup</button>
</form>
</div>
</div>

</body>

最佳答案

这不行:

return errorFields(); // Here you're trying to call a function with an array.

只需返回数组: return errorFields;

另一个问题是比较:

if (errors == "") { // This is not ok (it's always false), so, what you want to check is the length of errors.
return true;
} else {
provideFeedback(errors);
e.preventDefault();
return false
}

所以,检查长度:

if (errors.length === 0) {
return true;
} else {
provideFeedback(errors);
e.preventDefault();
return false
}

关于javascript - 提交表单时不显示错误类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49087024/

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