我遇到了簡單的密碼驗證問題。JavaScript驗證密碼錯誤
這些都是事情驗證確實
- 檢查,如果密碼少於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>
只是出於好奇,你爲什麼要施加15個字符的上限? – Blender
如果您在表單中使用按鈕,無論您使用Javascript做什麼,它都會默認提交。你能告訴我們這個函數是如何被調用的嗎?此外,你最初檢查'current.value'然後切換到'new1.value'這是故意的嗎? –
我其實有三個字段。 1.當前密碼2.新密碼3.重新輸入密碼。 –