I'm writing a password validator in JS (very basic, it's just an exercise), and when I try to validate for upper or lower cases I get a 'Valid Password' answer, even if there is not one or the other present, as long as there is a special character in it. I know why that happens, but what I'm trying to figure out is how to rule out the special characters while validating lower or upper case. Here's the code:
function ValidatePassword(input){
if(hasUppercase(input) && hasLowercase(input) && isLongEnough(input) && hasSpecialCharacters(input)){
console.log('The password is valid.');
}else if(!hasUppercase(input)){
console.log('The password needs at least one capital letter.');
}else if(!hasLowercase(input)){
console.log('The password needs at least one lowercase letter.');
}else if(!isLongEnough(input)){
console.log('The password needs to be at least 8 characters long.');
}else if(!hasSpecialCharacters(input)){
console.log('The password needs at least one 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 hasSpecialCharacters(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;
}
}
}
}
Anyways, I'd appreciate any help you can give me