Learning JavaScript - Password Validator - Problems with if/then statements


#1


Learn JavaScript - Password Validator exercise


My code below is set up to parse an inputted password to check if it is secure enough.
These are the following conditions for validity:

  1. Must have an uppercase character

  2. Must have a lower case character

  3. Must be 8 characters or longer

  4. Must have a special character

For the most part, my code returns the correct results with the exception of the special character pass. For example: if I run my function with isPasswordValid('aaaaa!'), my code returns the 'This password is valid!' message even though my password does not contain an uppercase character and isn't long enough.

Can someone take a look at my code to see why this is happening? Any help would be appreciated!
Also, if anyone replying has the time, could you give me some pointers on how to streamline the code?


function isPasswordValid(input) {
  if (!hasUpperCase(input)) {
    console.log('The password requires a capital letter.');
  }
  if (!hasLowerCase(input)) {
    console.log('The password requires a lower case letter.');
  }
  if (!isLongEnough(input)) {
    console.log('The password is not long enough.');
  }
  if (!hasSpecialCharacter(input)) {
    console.log('The password requires a special character.');
  }   
  if (hasUpperCase(input) && hasLowerCase(input) && isLongEnough(input) && hasSpecialCharacter(input)) {
    console.log('The password is valid.');
  } 
}

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) {
  for (var i=0; i<input.length; i++) {
    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('aaaaaaaaa!');


#2

Your validation functions are not correct:

function hasUpperCase(input) {
  for (var i = 0; i < input.length; i++) {
    if (input[i] === input[i].toUpperCase()) { // if i = 0 => you compare same character
      return true;
    }
  }
};

function hasLowerCase(input) {
  if(input.toUpperCase() != input) 
    return true;
  return false;
}; // of course, you can compare characters but I think it's better way

// if you need to use for loop:
function hasLowerCase(input) {
  for(var i=0; i<input.length; i++) {
    if(input.charAt(i) >= 'a' && input.charAt(i) <= 'z')
      return true;
  }
  return false;
}

function hasUpperCase(input) {
  if(input.toLowerCase() != input) 
    return true;
  return false;
}

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

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;
      }
    }
  }
}; // if input.length < specialCharacters.length ? or > ?

// you can use something like this:
function hasSpecialCharacter(input) {
  var specialCharacters = "!@#$%^&*,()-+";
  for (var i = 0; i < input.length; i++) {
    if (specialCharacters.indexOf(input.charAt(i)) != -1)
      return true;
  }
  return false;
};


Results:
isPasswordValid('aaaaaaaaa!');
The password requires a capital letter.

isPasswordValid('aaa');
The password requires a capital letter.
The password is not long enough.
The password requires a special character.

isPasswordValid('aaa!');
The password requires a capital letter.
The password is not long enough.

isPasswordValid('AAAAAAA!');
The password requires a lower case letter.

isPasswordValid('aaaAaaaaa!');
The password is valid.

So,
1. You forgot put return false; in your validation functions.
2. Not correct implementations of validation functions.
3. Don't forget put " ; " after function declaration.


#3

@denys.matsevych,
Function declarations DO NOT need a semicolon, but function STATEMENTS do:

Statement:
var a = function example() {
};

Declaration:
function example() {
}

Furthermore, I do not believe you need to 'return false' as the function will automatically return false if it isn't true.

Also, your code doesn't follow the actual lesson.


#4

I came up with exactly the same code. My password will validate correctly on all functions except hasLowerCase() when I add a special character which will always validate the password. To be clear, I can call:

isPasswordValid('aaa'); // evaluates to:
'The password requires a capital letter.' 'The password is not long enough.' 'The password requires a special character.'
or even:
isPasswordValid('AAA'); //evaluates to: 'The password needs a lowercase letter.' 'The password is not long enough.' 'The password requires a special character.'

But, I cannot get the lower case function to evaluate when I add a special character:

isPasswordValid('AAAAAAAAAAAAA!'); //evaluates to: 'The password is valid.'

Can someone clarify what we're doing wrong please? Here's my code: https://gist.github.com/66c0ea420a76332bc9809fa81c83e703


#5

I got the same problem with lowercase, uppercase and special characters.
Even I went with isPasswordValid('#');
Just one requirement that my password need to be more than 8 characters, not even asking for any uppercase, lowercase.

Please help!


#6

the logic in hasUpperCase validates passwords without upper case letters if it includes a number or special character in the input. Wouldn't a number match its toUpperCase() conversion?

You'd need to somehow exclude all numbers and special characters from the hasUpperCase().
Is that the way it's supposed to be?

input.charAt(i) >= 'a' && input.charAt(i) <= 'z'

Here is the rest of my code

var minLength = 3;

function isPasswordValid(input) {
  if (!hasUpperCase(input)) {
    console.log('The password requires a capital letter.');
  }
  if (!hasLowerCase(input)) {
    console.log('The password requires a lower case letter.');
  }
  if (!isLongEnough(input)) {
    console.log('The password is not long enough.');
  }
  if (!hasSpecialCharacter(input)) {
    console.log('The password requires a special character.');
  }   
  if (hasUpperCase(input) && hasLowerCase(input) && isLongEnough(input) && hasSpecialCharacter(input)) {
    console.log('The password is valid.');
  } 
}

function hasUpperCase(input) {
 for(var i = 0; i < input.length; i++){
   if(input[i] === input[i].toUpperCase()){
     return true;
// How do we validate that it's not a number or a special Character?
   }
 } 
}

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 >= minLength){
    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;
      }
    }
  }
}

var password = prompt('Please enter new password:');
isPasswordValid(password);

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.