簡體   English   中英

JavaScript錯誤中的密碼驗證

[英]Password Validation in JavaScript Error

我在進行簡單的密碼驗證時遇到了麻煩。

這些是驗證所做的事情

  • 檢查密碼是否少於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>

由於您沒有提到發生警報的情況,因此我猜測您正在使用表單中的按鈕來調用此函數。 不管onclick函數如何,它將自動提交表單,除非您按以下問題中所述設置按鈕的類型: 如何防止按鈕提交表單

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

如果您希望表單提交失敗,請嘗試在驗證失敗時返回false。

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

您需要根據需要從函數返回true (提交表單)或false (停止提交),還需要使用onclick="return on_submit('logon');" -甚至更好的是,應將其刪除並放上onsubmit="return on_submit('logon');" 在表單本身上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM