代碼是用JSP編寫的,應該驗證密碼並確認密碼的正確性,然後將提交事件繼續到Java servlet。但問題是,如果驗證腳本失敗,它不會顯示警報消息,也不會關注密碼輸入。我使用鉻,探險家和Eclipse的默認瀏覽器..只是將無法正常工作Javascript將不會與html代碼交互
function validate(event){
event.preventDefault();
var pattern = /^(?=.*[a-z].*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,32}$/;
var pwd = document.form.password.value;
var confPwd = document.fomr.confPass.value;
if(pwd.match(pattern)){
\t alert("Password must be between 8 to 32 characters,\n have at least one digit \n two lower and one upper case letters.");
document.form.password.focus();
return false;
}else if(pwd == confPwd){
alert("Passwords do not match. Please try again.");
document.form.password.focus();
document.form.confPass.focus();
return false;
}else{
document.form.submit();
}
}
<form name="form" method="post" action="RegisterPath" onSubmit="return validate(event);">
<table class="box">
<tr>
<td class="left">Password:</td>
<td><input type="password" name="password" placeholder="Enter password" required/></td>
</tr>
<tr>
<td class="left">Confirm Password:</td>
\t <td><input type="password" name="confPass" placeholder="Re-enter password" required/></td>
</tr>
</table>
</form>
'如果(PWD == confPwd)':應該不就是'如果(!PWD = confPwd)'?另外,'document.fomr','fomr'只是一個錯字? – AntonH
另外'document.fomr.confPass.value'是一個會妨礙其他函數運行的錯字。 – Phylogenesis
客戶端密碼驗證是一個安全循環漏洞。有人可以複製你的頁面,並刪除驗證密碼的JavaScript。如果您正在進行客戶端驗證,請確保您也在服務器端進行驗證。 – victor