[英]Password match confirmation
我正在尝试建立一个检查表单以确保我的密码和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>
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.