【问题标题】:Password match confirmation密码匹配确认
【发布时间】:2018-05-06 09:33:07
【问题描述】:

我正在尝试构建一个表单来检查以确保我的 pin 和 pswd 与表单匹配。但不确定我错过了什么。我对 WebDev 世界还很年轻,我花了很多时间试图找出一些真正简单的东西(在我的脑海中)。我已经尝试了这 5 种不同的方法,并且一直在简化它。这是我基本上从别人那里复制的东西,它对我不起作用。有小费吗?只需要获取两个输入来检查它们是否基本匹配。仅供参考:我正在处理的文件名为 initialsetup3SANDBOX.php(不确定是否重要)。

function myFunction() {
  var pass1 = document.getElementById("pass1").value;
  var pass2 = document.getElementById("pass2").value;
  if (pass1 != pass2) {
    //alert("Passwords Do not match");
    document.getElementById("pass1").style.borderColor = "#E34234";
    document.getElementById("pass2").style.borderColor = "#E34234";
  } else if {
    alert("Passwords Match!!!");
    document.getElementById("regForm").submit();
  }
}
<!DOCTYPE html>
<html>

<head>
</head>

<body>

  <form id="regform" action="/initialsetup3SANDBOX.php" method="post" onsubmit="return myFunction();">
    <input id="pass1" type="password" placeholder="Password" style="border-radius:7px; border:2px solid #dadada;"><br>
    <input id="pass2" type="password" placeholder="Confirm Password" style="border-radius:7px; border:2px solid #dadada;"><br>
  </form>
  <input type="submit" value="Submit">



</body>

</html>

【问题讨论】:

  • 你得到了什么结果?
  • else if { 语法不正确,应该是 else if (*condition*) { 或只是 else {
  • 什么都没有。没有得到错误。我只是点击提交,没有任何反应。

标签: javascript formvalidation-plugin password-confirmation


【解决方案1】:

3 件事。

  • &lt;input type="submit" value="Submit" /&gt; 应该包装成表格 标记,那么只有表单会提交。

  • 你应该使用else而不是else if

  • 另外,你必须return false。当密码不匹配时。 否则,表单操作无论如何都会发生。

function myFunction() {
  var pass1 = document.getElementById("pass1").value;
  var pass2 = document.getElementById("pass2").value;
  if (pass1 != pass2) {
    //alert("Passwords Do not match");
    document.getElementById("pass1").style.borderColor = "#E34234";
    document.getElementById("pass2").style.borderColor = "#E34234";
    return false;
  } else {
    alert("Passwords Match!!!");
    document.getElementById("regForm").submit();
  }
}
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <form id="regform" action="/initialsetup3SANDBOX.php" method="post" onsubmit="return myFunction();">
    <input id="pass1" type="password" placeholder="Password" style="border-radius:7px; border:2px solid #dadada;"><br>
    <input id="pass2" type="password" placeholder="Confirm Password" style="border-radius:7px; border:2px solid #dadada;"><br>
    <input type="submit" value="Submit" />
  </form>
</body>

</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2014-05-08
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多