0

我正在尝试构建一个表单来检查以确保我的 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>

4

1 回答 1

2

3件事。

  • <input type="submit" value="Submit" />应该包装到表单标签中,然后只有表单会提交。

  • 你应该使用elsenot 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>

于 2017-11-22T15:25:25.700 回答