gpt4 book ai didi

JavaScript 返回 false 表单验证不起作用

转载 作者:行者123 更新时间:2023-11-28 21:06:35 24 4
gpt4 key购买 nike

你能帮我吗,当验证失败时,我的脚本应该取消隐藏 p 标记中的错误消息并返回 false,但我的表单会提交。你能看出我的问题出在哪里吗?

function validate() {
if (document.emailForm.EMAIL_ADDRESS_.value.length==0) {
$('#errorNoAddress').fadeIn();
return false;
}
if (document.emailForm.EMAIL_ADDRESS_.value.length>0) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.emailForm.email.value;
if(reg.test(address) == false) {
$('#errorInvalidAddress').fadeIn();
return false;
}
}
document.emailForm.submit();
return true;
}

<form id="emailForm" name="emailForm" action="http://domain.com/formhandler.php" method="post">
<p id="errorNoAddress" class="error">Please enter an e-mail address.</p>
<p id="errorInvalidAddress" class="error">Please enter a valid email address.</p>
<label for="EMAIL_ADDRESS_">E-mail</label>

<input type="hidden" id="QUICK_SIGN_UP" name="QUICK_SIGN_UP" value="" />
<input type="image" src="btn-submit.gif" value="Submit" onclick="validate();" />
</form>

最佳答案

尝试<input type="image" src="btn-submit.gif" value="Submit" onclick="return validate();" />

关于JavaScript 返回 false 表单验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9704340/

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