I'm working on a registration page (form). If the two password fields don't match I want to display an alert but I don't want it to refresh after the alert is displayed and lose all the information in all the other fields. Ideally, I'd love it if it reset the two password fields only and kept everything else the same.
JavaScript Code:
function passwordMatch(){
if(document.getElementById('password1').value !== document.getElementById('password2').value){
alert("Passwords don't match!");
false;
}
}
I looked around and all I could find were solutions using PHP I'm not sure if I should switch to that but I prefer using JavaScript.
HTML Code
<form id = "testform" name = "testform" method = "post" action="" onsubmit="return validate(this);">
<p><label for="userFirstName">Fisrt Name:</label>
<input name = "userFirstName" type = "text" id = "userFirstName"/>
</p>
<p><label for ="userLastName">Last Name:</label>
<input name="userLastName" type = "text" id = "userLastName"/>
</p>
<p><label for = "userName">Username:</label>
<input name = "userName" type = "text" id = "userName"/>
</p>
<p><label for = "password">Enter Password:</label>
<input name = "password" type = "password" id = "password1"/>
</p>
<p><label for = "password2">Re-Enter Password:</label>
<input name = "password2" type = "password" id = "password2"/>
</p>
<p>
<input type = "submit" name="save" href = "login.php" value = "Register"/>
</p>
</form>