我在我的註冊表單中有一個密碼字段。以下是javascript驗證的代碼。單引號的javascript驗證
if(document.getElementById('txtPassword').value == '' || document.getElementById('txtPassword').value == 'Password'){
alert("Please enter Password");
document.getElementById('txtPassword').focus();
return false;
}
else if (document.getElementById('txtPassword').value.length < 8)
{
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
else if ((! document.getElementById('txtPassword').value.match(/[a-z]/)) || (! document.getElementById('txtPassword').value.match(/[A-Z]/))) {
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
else if (!document.getElementById('txtPassword').value.match(/\d+/)) {
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
else if (document.getElementById('txtPassword').length < 8)
{
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
我需要檢查單引號。如果用戶在密碼字段中輸入單引號,並彈出輸入錯誤「避免在此字段中使用單引號」。
如何做到這一點?
而不是仰視元素及其在每個'if'聲明價值,只是做一次之初並將其存儲在一個變量。 – 2012-08-04 09:32:43
[如何判斷一個字符串是否包含javascript中的某個字符?]的可能重複(http://stackoverflow.com/questions/4444477/how-to-tell-if-a-string-contains-a-certain-字符在JavaScript) – 2012-08-04 09:33:28