简体   繁体   English

重新输入密码验证不起作用

[英]retype password verification not working

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 希望罗宾

Your form will submit no matter what, because you are not returning false from the validating function on error. 您的表单无论如何都将提交,因为您不会在错误时从验证函数返回false。 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. 当前,您没有为validate()指定返回值,并且它被解释为始终返回true,因此将提交表单。 You don't need to call submit() from your function, simply return true if everything is ok. 您无需从函数中调用submit() ,只要一切正常就可以返回true。

The onsubmit handler on your <form> is looking for a return value, but is not given one from your validate function. <form>上的onsubmit处理程序正在寻找返回值,但您的validate函数未提供该返回值。 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): 而不是在函数中调用submit() ,您应该返回true或false,具体取决于表单是否通过验证(如果函数返回false ,则等效于onsubmit="false" ,这将取消提交):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM