2016-11-09 21 views
0

當我把數字放在我的「密碼」中時,沒有大寫的錯誤信息。幫幫我!!!您可以忽略大部分代碼。我的問題是爲什麼當我把數字放在密碼中時,控制檯不會記錄「不正確的密碼,請輸入一個大寫字母。」 (你輸入密碼的地方在代碼的最後一行)謝謝你的幫助!爲什麼javascript的.toUpperCase函數包含數字?

var specialCharacters = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", 
"[", "]"]; 

function isPasswordValid(input) { 
if (hasUpperCase(input) && hasLowercase(input) && isLongEnough(input) && hasSpecialCharacter(input)) { 
console.log("The password is valid."); 
} 

if (!hasUpperCase(input)) { 
console.log("Incorrect password. Please put a uppercase letter."); 
} 

if (!hasLowercase(input)) { 
console.log("Incorrect password. Please put a lowercase letter."); 
} 

if (!isLongEnough(input)) { 
console.log("Incorrect password. Please increase the length of your password to 8 characters."); 
} 

if (!hasSpecialCharacter(input)) { 
console.log("Incorrect password. Please put a special character."); 
} 
} 

function hasUpperCase(input) { 
for (var i = 0; i < input.length; i++) { 
if (input[i] === input[i].toUpperCase()) { 
    return true; 
} 
} 
} 

function hasLowercase(input) { 
for (var i = 0; i < input.length; i++) { 
if (input[i] === input[i].toLowerCase()) { 
    return true; 
} 
} 
} 

function isLongEnough(input) { 
if (input.length >= 8) { 
return true; 
} 
} 

function hasSpecialCharacter(input) { 
for (var i = 0; i < input.length; i++) { 
for (var j = 0; j < specialCharacters.length; j++) { 
    if (input[i] === specialCharacters[j]) { 
    return true; 
    } 
} 
} 
} 

isPasswordValid(""); 
+0

輸入可能是一個字符串,所以這個數字實際上是一個字符串。 – elclanrs

回答

0

您的輸入是一個字符串。如果您使用toUpperCase()方法,它會忽略數字,並只是將小寫字母/字符轉換爲大寫。

0

「沒有大寫的錯誤信息」因爲您沒有檢查大寫字母,所以您只是測試字符串相等。

"1" == "1".toUpperCase() 

toUpperCase()不會去除數字或其他非字母字符。

如果你想測試一個大寫字母,實際上測試一個大寫字母。

可以使用regular expression test做到這一點:

if(!(/[A-Z]/).test(input[i])){ 
    //No uppercase letters found 
} 

[A-Z]告訴表達式查找所提供的字符集內的字符(在這種情況下,資金從A到資本Z)

0

嘗試添加更多,如果參數if語句是這樣的()

function hasUpperCase(input) { 
 
    for (var i = 0; i < input.length; i++) { 
 
    if (input[i] === input[i].toUpperCase() && isNaN(parseInt(input[i]))) { 
 
     return true; 
 
    } 
 
    } 
 
}

相關問題