I have the following code to validate email and password repeat, and keep getting error that "Password does did match" even when I enter the same on form submit. I have the following code. Please help fix as I am new and learning. Simple steps will assist. Thanks in advance.
----html----
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<form
id="userForm"
action="signup_otp.html"
method="POST"
name="userForm"
onsubmit="return(validate());"
>
</body>
----script.js---------
//Validation of email and password
function validate() {
document.getElementById("emailAlert").innerHTML="";
var emailVal = /^([A-Za-z0-9_\-\.]) \@([A-Za-z0-9_\-\.]) \.([A-Za-z]{2,4})$/;
if(!document.userForm.email_id.value.match(emailVal)) {
document.getElementById("emailAlert").innerHTML="Enter a valid email format";
//document.getElementById("email_id").focus();
document.userForm.email_id.focus();
//alert(emailAlert);
return false;
}
alert("check passwd")
document.getElementById("passAlert").innerHTML="";
var pw1 = document.getElementById("psw");
var pw2 = document.getElementById("psw_repeat");
console.log(pw1); // console shows null value
console.log(pw2);
if(pw1 != pw2)
{
//alert("Passwords did not match");
document.getElementById("passAlert").innerHTML="Password did not match";
document.userForm.psw.focus();
return false;
} else {
alert("Password created successfully");
return false;
}
return true;
}
CodePudding user response:
var pw1 = document.getElementById('psw').value
CodePudding user response:
I think this link can help you a lot : enter link description here
