Reputation: 817
I used the following code:
<script type="text/javascript">
function validate() {
if ((document.changepass.newpassword.value != '') &&
(document.changepass.retypenewpassword.value ==
document.changepass.newpassword.value)){
document.changepass.retypenewpassword.blur();
document.changepass.submit();
}
}
</script>
<form name="changepass" method="POST" action="changepassword.jsp" onsubmit="return validate();">
<table align="center">
<tr>
<td>New Password:</td>
<td><input type="password" name="newpassword" size="20"
style="width: 145px" maxlength="10"></td>
</tr>
<tr>
<td>Retype New Password:</td>
<td><input type="password" name="retypenewpassword" size="20"
style="width: 144px" maxlength="10"></td>
</tr>
<tr>
<td><input type="submit" value="Change Password" name="operation" ></td>
<td><input type="reset"></td>
</tr>
</table>
</form>
but when I'm giving unmatched entry then also it getting changed.i mean the validate function is not getting called. plz help
in hope robin
Upvotes: 1
Views: 1347
Reputation: 124778
Your form will submit no matter what, because you are not returning false from the validating function on error. It should be:
function validate() {
var d = document.changepass;
if((d.newpassword.value != '') && (d.retypenewpassword.value == d.newpassword.value)) {
return true;
}
return false;
}
At current, you are not specifying a return value for validate()
, and it is interpreted as always returning true, so the form gets submitted. You don't need to call submit()
from your function, simply return true if everything is ok.
Upvotes: 3
Reputation: 94173
The onsubmit
handler on your <form>
is looking for a return value, but is not given one from your validate
function. Instead of calling submit()
in the function, you should return true or false, depending on if the form validates (if the function returns false
, that is equivalent to onsubmit="false"
, which will cancel the submission):
function validate()
{
if ((document.changepass.newpassword.value != '') && (document.changepass.retypenewpassword.value != document.changepass.newpassword.value))
{
// A password is given but it doesn't match
// Perhaps you want to alert() an error message here to tell the user why the form doesn't validate?
return false;
}
return true;
}
Upvotes: 2