简体   繁体   English

JavaScript错误中的密码验证

[英]Password Validation in JavaScript Error

I am having trouble with my simple password validations. 我在进行简单的密码验证时遇到了麻烦。

These are the things that validation does 这些是验证所做的事情

  • check if password is less than 8 characters 检查密码是否少于8个字符
  • check if it is more than 15 characters 检查是否超过15个字符
  • check if new password and retyped are mismatched 检查新密码和重新输入的密码是否不匹配

The form submits when two passwords are matched even if less than or greater than the validations 当两个密码匹配时(即使小于或大于验证),也会提交表单

Please see my code: 请查看我的代码:

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

Since you don't mention the alert occuring, my guess is that you are calling this function with a button from within the form. 由于您没有提到发生警报的情况,因此我猜测您正在使用表单中的按钮来调用此函数。 That will automatically submit the form, regardless of the onclick function, unless you set the type on the button as described in this question: How to prevent buttons from submitting forms 不管onclick函数如何,它将自动提交表单,除非您按以下问题中所述设置按钮的类型: 如何防止按钮提交表单

<input id="logonButton" class="submit"
                    type="button" name="Submit" value="Confirm" tabindex="3"
                    onclick="on_submit('logon')" />

if you want the form to fail on submission try returning false when validation fails. 如果您希望表单提交失败,请尝试在验证失败时返回false。

        if (strMissingInfo != "") {
            alert(strMissingInfo);
            return false;
        }

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');" 您需要根据需要从函数返回true (提交表单)或false (停止提交),还需要使用onclick="return on_submit('logon');" - or even better you should remove it and put onsubmit="return on_submit('logon');" -甚至更好的是,应将其删除并放上onsubmit="return on_submit('logon');" on the form itself. 在表单本身上。

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

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