0

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

3 Answers3

1

change upperCase and lowerCase functions as mentioned below

function hasLowercase(str) {
    return (/[a-z]/.test(str));
}

function hasUppercase(str) {
    return (/[A-Z]/.test(str));
}
samnu pel
  • 914
  • 5
  • 12
  • can also change hasSpecialCharacters to use a regex - I think `/[@%&<>!;:#,._\\\/\*\-\+\(\)\$\^]/` should work for you – jdubjdub Aug 28 '17 at 22:34
1

The password is valid because hasUppercase and hasLowercase are returning true for example: "@".toUpperCase() === "@". you can move "specialCharacters" to out and change the functions

var specialCharacters = ["/", "*", "-", "+", "_", "@", "%", "&", "<", ">", "!", "(", ")", "$", "^", "\\", "#", ".", ",", ";", ":"];

function hasUppercase(input) {
    for (var i = 0; i < input.length; i++) {
        if (specialCharacters.indexOf(input[i]) == -1 && input[i] === input[i].toUpperCase()) {
            return true;
        }
    }
}

function hasLowercase(input) {
    for (var i = 0; i < input.length; i++) {
        if (specialCharacters.indexOf(input[i]) == -1 && input[i] === input[i].toLowerCase()) {
            return true;
        }
    }
}
Mario Lopes
  • 125
  • 7
  • BUT PAY ATTENTION: you need add all special characters, including numbers. – Mario Lopes Aug 28 '17 at 22:19
  • I think you'd better use regular expression – Mario Lopes Aug 28 '17 at 22:32
  • I'm not that far ahead with JS, so I don't know how to use them. This is an exercise from Codeacademy that I tried to improve on by unifying all validation into one big conditional. But it comes with the added difficulty of having to find a different way to validate special characters. Thanks for the help!! – Martin Scola Aug 30 '17 at 00:49
0

As mentioned by @SergioEscudero above Using regex makes the easier task. Here is a working version of your code

function ValidatePassword(input){
    if(isLongEnough(input) && hasUppercase(input) && hasLowercase(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].match(/^[A-Z]*$/)){
            return true;
        }
    }
}

function hasLowercase(input){
    for (var i = 0; i < input.length; i++){
        if(input[i].match(/^[a-z]*$/)){
            return true;
        }
    }
}

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

function hasSpecialCharacters(input){
for(var i = 0; i < input.length; i++){
    if(input`enter code here`[i].match(/[0-9-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]/i)){
        return true;
     }

  }
}
ValidatePassword('sdgdgdgdgdgdD{')
ValidatePassword('sdgdgdgdgdgdD')
ValidatePassword('DDDDDDDDDDDDDDDD#{')
ValidatePassword('sdgdgdgdgdgd{')
//The password is valid.
//The password needs at least one special character.
//The password needs at least one lowercase letter.
///The password needs at least one capital letter.
Danstan
  • 1,501
  • 1
  • 13
  • 20