Password Validator Issue


#1

Hi there! I am having a problem with my hasUpperCase() and hasLowerCase() functions. I believe the code is also checking the special characters (which obviously are equal to their "upper" and "lower" cases). Because of that, these functions do not respond with the correct info. Please let me know how you wrote your functions to get around this problem. This is my code:

function isPasswordValid(input) {
if(hasUppercase(input) && hasLowercase(input) && isLongEnough(input) && hasSpecialCharacter(input) ) {
console.log("The password is valid")
}
if(!hasUppercase(input)) {
console.log("Your password needs a capital letter");
}
if(!hasLowercase(input)) {
console.log("Your password needs a lowercase letter");
}
if(!isLongEnough(input)) {
console.log("Your password is not long enough")
}
if(!hasSpecialCharacter(input)) {
console.log("Your password needs 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 j = 0; j < input.length; j++) {
if(input[j] === input[j].toLowerCase()) {
return true
}
}
}

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

function hasSpecialCharacter(input) {
var specialCharacters = ['!', '@', '#', '$', '%', '^', '&', '*']
for (var i = 0; i < input.length; i++) {
for (var j = 0; j < specialCharacters.length; j++) {
if (input[i] === specialCharacters[j]) {
return true;
}
}
}
}

isPasswordValid("JjfieJefji!")


#2

Did you check other topics on this forum section? link

the best fix is to use regular expression/.search as shown here: