JavaScript错误中的密码验证

JavaScript错误中的密码验证,javascript,html,Javascript,Html,我的简单密码验证有问题 这些就是验证所做的事情 检查密码是否少于8个字符 检查是否超过15个字符 检查新密码和重新键入的密码是否不匹配 当两个密码匹配时,即使小于或大于验证,表单也会提交 请查看我的代码: function on_submit(dest) { var objForm = document.LogonForm; var strMissingInfo = ""; if (dest == 'logon') { if (objForm.c

我的简单密码验证有问题

这些就是验证所做的事情

  • 检查密码是否少于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();
            }
        }
    }
}
提交(dest)时的
功能{
var objForm=document.LogonForm;
var strmissingfo=“”;
如果(dest=='logon'){
如果(objForm.current.value!=“”){
if(objForm.new1.value.length<8 | | objForm.new1.value.length>15){
strmissingfo+=“\n密码长度必须为8到15个字符”;
}else if(objForm.retype.value!=objForm.new1.value){
strmissingfo+=“\n密码不匹配!”;
}
如果(strmissingfo!=“”){
警报(strmissingfo);
}否则{
警报(strmissingfo);
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函数,除非您按照此问题中的说明在按钮上设置类型:


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

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

您需要根据需要从函数中返回
true
(提交表单)或
false
(停止提交),并使用
onclick=“提交时返回('logon');”
——或者更好的是,您应该将其删除并将
onsubmit=“提交时返回('logon');“
放在表单本身上。

只是出于好奇,为什么要设置15个字符的上限?如果您在表单中使用按钮,则默认情况下,无论您如何使用Javascript,它都会提交。你能告诉我们如何调用这个函数吗?另外,您最初检查
current.value
,然后切换到
new1.value
这是故意的吗?我实际上有三个字段。1.当前密码2。新密码3。重新键入密码。如何调用提交时的
_submit
?单击按钮..是的,单击提交按钮后我调用函数。谢谢,好的,那么您需要将type=button添加到该按钮的标记中。否则,它将自动提交表单。你的函数可能工作,也可能不工作,很难说清楚,因为我不知道你所有的变量都是什么,但它现在还没有运行:)即使验证结果令人满意,它也不会提交。嗯,你确定它符合提交语句吗?它是发出警报还是抛出错误?就像我说的,我认为你的函数可能还有其他问题,但这至少是第一个问题。它会发出警报。我会再次检查我的功能。谢谢:)在我的功能区,它必须返回什么吗?错误仍然是一样的。即使密码小于8或大于15,表单也会提交。
        if (strMissingInfo != "") {
            alert(strMissingInfo);
            return false;
        }