【问题标题】:retype password verification not working重新输入密码验证不起作用
【发布时间】:2010-01-04 08:21:46
【问题描述】:

我使用了以下代码:

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

但是当我给出不匹配的条目时,它也会被改变。我的意思是验证函数没有被调用。 请帮忙

希望 知更鸟

【问题讨论】:

  • 您需要使用 101010 按钮格式化您的代码。我猜你有一些 HTML 标签,这些标签正在被删除。
  • 这与问题无关,但您可能确定服务器端代码也提供相同的控制。 Javascript验证很容易被跳过,你不能依赖它。

标签: javascript


【解决方案1】:

无论如何,您的表单都会提交,因为您不会在错误时从验证函数返回 false。应该是:

function validate() {
    var d = document.changepass;
    if((d.newpassword.value != '') && (d.retypenewpassword.value == d.newpassword.value)) {
        return true;
    }
    return false;
}

目前,您没有为 validate() 指定返回值,它被解释为始终返回 true,因此表单被提交。您不需要从您的函数中调用submit(),如果一切正常,只需返回 true。

【讨论】:

    【解决方案2】:

    &lt;form&gt; 上的 onsubmit 处理程序正在寻找返回值,但没有从您的 validate 函数中获得返回值。与其在函数中调用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;
    }
    

    【讨论】:

    • 如果逻辑错误((document.changepass.newpassword.value == '')|| document.changepass.retypenewpassword.value != document.changepass.newpassword.value))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2016-08-16
    相关资源
    最近更新 更多