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

时间:2012-03-14 14:54:12

标签: javascript forms validation return

你可以帮助我,当验证失败时,我的脚本应该取消隐藏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>

1 个答案:

答案 0 :(得分:3)

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