密码匹配确认



我正在尝试构建一个检查表格,以确保我的PIN和PSWD匹配表单。但不确定我缺少什么。我在WebDev世界中年轻一点,我花了很多时间试图找出真正简单的东西(在我的脑海中)。我尝试了这5种不同的方法,并继续愚弄它。这是我从其他人那里复制的东西,这对我不起作用。有小费吗?只需要让两个输入本质上匹配它们是否匹配。仅供参考:我正在处理的我的文件称为ritialsetup3sandbox.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>

3件事。

  • <input type="submit" value="Submit" />应包裹成形式标签,然后只会提交表格。

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

最新更新