0

我的简单密码验证有问题。

这些是验证所做的事情

  • 检查密码是否少于 8 个字符
  • 检查是否超过 15 个字符
  • 检查新密码和重新输入的密码是否不匹配

当两个密码匹配时提交表单,即使小于或大于验证

请看我的代码:

function on_submit(dest) {

    var objForm = document.LogonForm;
    var strMissingInfo = "";

    if (dest == 'logon') {

        if (objForm.current.value != "") {

            if (objForm.new1.value.length < 8 || objForm.new1.value.length > 15) {
                strMissingInfo += "\n   Password must be 8 to 15 characters long";

            } else if (objForm.retype.value != objForm.new1.value) {
                strMissingInfo += "\n   Password mismatch!";

            }

            if (strMissingInfo != "") {
                alert(strMissingInfo);

            } else {
                alert(strMissingInfo);
                objForm.action.value = dest;
                objForm.submit();
            }
        }
    }
}

--- HTML部分

<input type="password" id="current" name="current"
                                onKeyPress="return isNumberKey(event)" style="width: 180px"
                                tabindex="1" required/>

<input type="password" id="new1" name="new1"
                                onKeyPress="return isNumberKey(event)" style="width: 180px"
                                tabindex="2" required />

<input type="password" id="retype" name="retype"
                                onKeyPress="return isNumberKey(event)" style="width: 180px"
                                tabindex="3" required />

<a href="javascript:;">
                    <input id="logonButton" class="submit"
                        type="submit" name="Submit" value="Confirm" tabindex="4"
                        onclick="on_submit('logon')" />
                    </a>
4

3 回答 3

0

You need to return either true (submit the form) or false (stop submission) as necessary from the function, and also use onclick="return on_submit('logon');" - or even better you should remove it and put onsubmit="return on_submit('logon');" on the form itself.

于 2013-05-20T03:33:00.293 回答
0

由于您没有提及发生的警报,我猜您正在使用表单中的按钮调用此函数。这将自动提交表单,无论 onclick 功能如何,除非您按照以下问题所述设置按钮的类型:如何防止按钮提交表单

<input id="logonButton" class="submit"
                    type="button" name="Submit" value="Confirm" tabindex="3"
                    onclick="on_submit('logon')" />
于 2013-05-20T03:14:47.790 回答
0

如果您希望表单在提交时失败,请尝试在验证失败时返回 false。

        if (strMissingInfo != "") {
            alert(strMissingInfo);
            return false;
        }
于 2013-05-20T03:16:08.223 回答