I'm trying to build a form that checks to make sure my pin and pswd match for a form. But not sure what I'm missing. I'm a little young to the WebDev world and I have spent a stupid amount of time trying to figure out something that should really be simple (in my head). I've tried this 5 different ways and keep dumbing it down. Here is something i essentially copied from someone else and it doesn't work for me. Any tips? Just need to get the two inputs to check if they match essentially. FYI: my file that I'm working on IS called initialsetup3SANDBOX.php (not sure if that matters).
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 things.
<input type="submit" value="Submit" />
should be wrapped into form tag, then only the form will submit.
You are supposed to use else
not else if
.
Also, You have to return false
. when the password doesn't match. otherwise, the form action will happen anyway.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.