2016-11-09 104 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; 
 
    } 
 
    } 
 
}