Problem with password if there're digits in it


#1

I passed this course and my program runs, but there is a logical problem with it. If I enter a string with digits (0-9) then hasUpperCase() and hasLowerCase() functions return true even if this string has no such letters. In the exersize there's no explanation of that issue, so the password 2ttjkhjhh* is correct.
Should I change my code in this functions to check if there are digits in the input or leave it according to the exersize?

Here is my full code.

function isPasswordValid(input) {
  if (hasUppercase(input) && hasLowercase(input) && isLongEnough(input) && hasSpecialCharacters(input)) {
    console.log('The password you entered is valid!');
  } 
  
  if (!hasUppercase(input)) {
    console.log('The password must contain at least one capital letter!');
  } 
  
  if (!hasLowercase(input)) {
    console.log('The password must contain at least one lowercase letter!');
  } 
  
  if (!isLongEnough(input)) {
    console.log('The password you entered is too short!');
  }
  
  if (!hasSpecialCharacters(input)) {
    console.log('The password must contain at least one special character!');
  }
} 

function hasUppercase(input) {
  for (var i = 0; i < input.length; i++) {
    if (input[i] === input[i].toUpperCase()) {
      return true;
    }
  }
  
  return false;
}

function hasLowercase(input) {
  for (var i = 0; i < input.length; i++) {
    if (input[i] === input[i].toLowerCase()) {
      return true;
    }
  }
  
  return false;
}

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

function hasSpecialCharacters(input) {
  var specialChars = " !\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~";
  
  for (var i = 0; i < input.length; i++) {
    if (specialChars.indexOf(input[i]) != -1) {
      return true;
    }
  }
  
  return false;
}

isPasswordValid('2ttjkhjhh*');

Numbers + Special Characters mistaken for uppercase
#2

well, good challenge to build this, right?

You could isNaN to expand your hasUppercase function and deal with the numbers


#3

OK, I used this function, thank you for your tip. But now I have the same problem with special characters. Possible solution is to create function which creates array of special characters in it and checks if input[i] is in this array. And then I can use this function where it is necessary. Am I right?


#4

oh, then my suggested solution is not really good. Hm... i was thinking maybe you can simply check if the character you are dealing with is alphabetic, but javascript doesn't seem to have a build in function for this.

Okay, we could use .search():

console.log(input.search(/[A-Z]/))
console.log(name.search(/[a-z]/))

one for uppercase one for lowercase. search will return 1 if the letter is present and -1 if not, that should be useful for a if comparison, right?

This is the easiest solution i can think of.....

But regex is really difficult to understand


Password Validator Issue
#5

Well, I know about regexes and this regex is very simple. I think this is nice solution... but of course it's not for the beginners. Thank you for your help :slight_smile:


#6

good, then i don't have to explains this, phew :stuck_out_tongue:

that much is true.

thank you :slight_smile:

No, but like is said JS has no built in function to check if a character is alphabetic, that would make it a lot easier. Then a simply loop and this built in function could do the trick. But i couldn't think of a better solution

You're welcome :slight_smile:


#7

The .search() function worked for me, but I had to use the ! operator, and I'm confused why. Did I do something backwards here, or am I just understanding the logic of the search() function wrong?

function hasUpperCase(input) {
  
  for (var i=0; i<input.length; i++) {
    if (!input[i].search(/[A-Z]/)) {
        return true;
        }
  }
}

function hasLowerCase(input) {
  
  for (var i=0; i<input.length; i++) {
    if (!input[i].search(/[a-z]/)) {
        return true;
        }
  }
}

#8

thankfully there is an entire documentation page on search