我在密碼錶單上提供驗證功能。我需要能夠實施一些驗證規則,並在提交時對它們進行檢查。現在,我的代碼是健全的,但我認爲他們可能是在我的代碼中的一些邏輯錯誤,我太累了,注意多重驗證檢查 - 邏輯錯誤
這裏的JavaScript的(太咖啡機!):
<script type="text/javascript">
<!--
function validate(registerForm)
registerForm.onsubmit=function()
{
var pw1 = document.forms["register"]["password1"].value;
var pw2 = document.forms["register"]["password2"].value;
//Check values are present in both fields
if(pw1 == '' || pw2 == '')
{
alert("Please enter your password twice.");
return false;
}
//Check there no spaces
else if(document.forms["register"]["password1"].value.indexOf(invalid) > - 1)
{
alert("Spaces are not allowed in passwords!");
return false;
}
//Check passwords are the same
else
{
if(pw1 != pw2)
{
alert("The passwords you entered were not the same. Please try again!");
return false;
}
//Accept passwords
{
alert("Password accepted!");
return true;
}
}
}
-->
</script>
而且HTML格式去與它:
<form id="register">
<label for="username">Username</label>
<input type="text" class="input_text" name="username" id="name" placeholder="e.g. AberLibrary01" />
<br />
<label for="password">Password</label>
<input type="text" class="input_text" name="password1" id="password1" placeholder="e.g. aber01" />
<br />
<label for="re-enterpassword">Re-enter password</label>
<input type="text" class="input_text" name="password2" id="password2" placeholder="e.g. aber01" />
<input type="submit" class="button" value="Register" />
</form>
<script type="text/javascript">
<!--
new validate(document.forms['register']);
-->
</script>
任何想法的可愛的StackOverflow社區?確切的問題是,它不會檢查密碼中的空格或輸入的兩個密碼是否相同。它成功地檢查了兩個密碼字段中至少有一些內容。
感謝丹
有什麼確切的問題? –
我忘了提及它,它已成功檢查密碼字段中是否有內容,但不檢查是否有空格或兩個密碼相互匹配! – WebDevDanno