为什么javascript's . touppercase函数包含数字?
Why does the javascript's .toUpperCase function include numbers?
当我在"password"中输入数字时,没有大写错误消息。帮助! !您可以忽略大部分代码。我的问题是,为什么当我在密码中输入数字时,控制台不会记录"密码不正确"。请用大写字母。"(输入密码的位置在代码的最后一行)谢谢你的帮助!
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("");
您的输入是一个字符串。如果您对它使用toUpperCase()
方法,它将忽略数字,并将小写字母/字符转换为大写字母/字符。
"there is no uppercase error message"因为您没有检查大写字母,您只是在测试字符串是否相等。
"1" == "1".toUpperCase()
toUpperCase()
不去掉数字或其他非字母字符。
如果你想测试一个大写字母,实际上测试一个大写字母。
您可以使用正则表达式测试来完成:
if( !(/[A-Z]/).test(input[i]) ){
//No uppercase letters found
}
[A-Z]
告诉表达式在提供的字符集(在本例中是大写a到大写Z)中查找一个字符
尝试为if语句添加更多的if参数,像这样()
function hasUpperCase(input) {
for (var i = 0; i < input.length; i++) {
if (input[i] === input[i].toUpperCase() && isNaN(parseInt(input[i]))) {
return true;
}
}
}
相关文章:
- 此行是否包含函数声明
- 在Debian中运行包含load()和print()函数的JS脚本
- 在包含tx.executeSql的每个函数之后显示警报
- 不执行包含的页面的Javascript onload函数
- 从我自己的脚本中包含的Javascript文件中调用一个函数
- javascript函数包含两个元素和web音频api
- 是否可以将我网站上的普通javascript和css函数包含到我的angularjs应用程序中
- 如何将服务注入链接函数包含指令
- JavaScript 函数包含可以相互访问的对象
- Rethinkdb谓词函数“包含”多个值不起作用
- 将参数传递给javascript函数,该函数包含一个'-'
- 协助javascript函数包含在循环(PHP)中
- 我如何改进这个toggleClass函数包含基本重复的逻辑
- 如果 JS 函数包含在 HTML 中,我如何在浏览器控制台中全部使用 JS 函数
- 为什么javascript's . touppercase函数包含数字?
- Jquery不返回通过PHP require()函数包含的元素
- 在对象中找不到函数包含
- 传递javascript动态参数的函数包含在字符串中
- Jquery函数包含外部变量,但不能读取
- 如何将匿名函数包含到RequireJS依赖项中