繁体   English   中英

为什么我的表单验证不起作用(onsubmit不触发)?

[英]Why is my form validation not working (onsubmit not triggering)?

我正在编写一个简单的注册屏幕,该屏幕允许用户输入其电子邮件地址和密码。 作为标准,我让用户输入两次电子邮件地址和密码进行确认。 但是,我表单上的onsubmit属性似乎没有执行。

这是代码:

领域

 <form name="form" id="form" action="" onsubmit="return validateForm()" class="form col-md-12 center-block" method="POST">
     <div class="form-group">
          <input type="text" class="form-control input-lg" name="name" id="name" placeholder="Full Name">
     </div>
     <div class="form-group">
          <input type="text" class="form-control input-lg" name="email" id="email" placeholder="Email Address">
     </div>
     <div class="form-group">
          <input type="text" class="form-control input-lg" name="cemail" id="cemail" placeholder="Confirm Email Address">
     </div>
     <div class="form-group">
          <input type="password" class="form-control input-lg" name="password" id="password" placeholder="Password">
     </div>
     <div class="form-group">
          <input type="password" class="form-control input-lg" name="cpassword" id="cpassword" placeholder="Confirm Password">
     </div>
     <div class="form-group">
          <button class="btn btn-primary btn-lg btn-block" form="form" type="submit" name="register" id="register">Register</button>
          <span class="pull-right"><a href="login.php">Login</a></span><br>
     </div>
 </form>

的JavaScript

<script>
    function validateForm() {
      if (isSame(document.getElementById("email"), document.getElementById("cemail"))
       && isSame(document.getElementById("password"), document.getElementById("cpassword"))) {
        return true;
    } else {
      alert("Confirmation fields do not match, please retype and try again.");
      return false;
    }

    function isSame(elementA, elementB) {
      if (elementA.value.trim() == elementB.value.trim()) return true;
      else return false;
    }

    //ignore this 
    function submitForm() {
      document.getElementById("form").submit();
    }
</script>

我尝试了尽可能多的调试,但是似乎我的提交按钮不会触发表单的onsubmit 我已经查看了请求日志,但是它很好地发布了数据。

先感谢您!

您的代码缩进不正确: validateForm函数的末尾缺少}

有一个语法错误。 第一个功能没有右括号。

另外,您的isSame函数不需要if语句。

希望这会有所帮助:

JS:

function validateForm() {
    if(isSame(document.getElementById("email"), document.getElementById("cemail"))
      && isSame(document.getElementById("password"), document.getElementById("cpassword"))) {
        return true;
    }else{
        alert("Confirmation fields do not match, please retype and try again.");
        return false;
    }
}

function isSame(elementA, elementB) {
  return elementA.value.trim() == elementB.value.trim();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM