javascript密码输入验证启动模式

时间:2019-01-10 19:40:21

标签: javascript

我正在尝试检查两个(密码)输入字段是否相同。如果不是,则应该启动模态。 我无法使它正常工作。有人可以建议我解决方案还是告诉我我做错了什么。 谢谢。

function checkEmail(theForm) {
 if (theForm.EMAIL_1.value != theForm.EMAIL_2.value)
  {
   $('#myModal').modal('show');
  }
}
<form action="modal.php" onsubmit="return checkEmail(this);" id="lop">
<p> Enter Your Email Address:<br>
<input type="TEXT" name="EMAIL_1" size="20" maxlength="20"> 
<br>
Please Confirm Your Email Address:
<br>
<input type="TEXT" name="EMAIL_2" size="20" maxlength="20"> 
<br>
<input type="SUBMIT" value="Send Address!"></p> 
</form>


<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header alert-danger">
        <h5 class="modal-title w-100  text-center" id="exampleModalLongTitle">Error</h5>

      </div>
      <div class="modal-body">
        <p class="text-danger text-center">Password does not match</p>
      </div>
      <div class="modal-footer justify-content-center">
      <button type="button" class="btn btn-secondary" data- 
 dismiss="modal">Cancel</button>

      </div>
    </div>
  </div>

2 个答案:

答案 0 :(得分:1)

如果您希望javascript进行输入检查,则需要停止提交表单。

function checkEmail(e) {
 e.preventDefault();
 const theForm = e.currentTarget;
 if (theForm.EMAIL_1.value != theForm.EMAIL_2.value)
  {
   $('#myModal').modal('show');
   return true;
  }
  theForm.submit(); // same password
}

然后将事件传递给js函数:

<form action="modal.php" onsubmit="return checkEmail(event);" id="lop">

答案 1 :(得分:0)

我可以用php脚本解决问题:

<?php



if (isset($_POST['submit'])) {
    $password = $_POST['password'];
    $password2 = $_POST['password2'];

    if ($password != $password2) {
        echo "<script>

$(document).ready(function(){
 $('#myModal').modal('show');
      });
</script>";
    }
}

?>