开发者

retype password verification not working

开发者 https://www.devze.com 2022-12-15 04:22 出处:网络
I used the following code: <script type=\"text/javascript\"> function validate() { if ((document.changepass.newpassword.value != \'\') &&

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>
    &l开发者_Python百科t;/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


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号