Numbers + Special Characters mistaken for uppercase


#1

So if I build this project the way that the instructions say, numbers and special characters are mistaken for uppercase letters. I read one of the other threads and saw that they was able to work around this using isNaN and search, but thus far I haven't learnt any of this.

Is this just bad instruction or am I missing something?


#2

Any change you read this topic?

Let me have a look at the course, we decided to ditch isNaN since it wouldn't help us with special characters.

Well, the design of the program is quit okay, it is just that user toUpperCase will also evaluate true for numbers and symbols. That is a issue


#3

I did read it but I don't understand the .search function :frowning: (I'm brand new to JavaScript)

And tbh everything works fine except the toUpperCase/toLowerCase thing.

This is the code that I've written, what would I need to do to make this work?

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;
}
}
}
}

function isPasswordValid(input) {
if (hasUppercase(input)) {
console.log("Password contains an uppercase letter");
}

if (!hasUppercase(input)) {
console.log("The Password contains no uppercase letters");
}

if (hasLowerCase(input)) {
console.log("Password contains a lowercase letter");
}

if (!hasLowerCase(input)) {
console.log("Password contains no lowercase letters");
}

if (isLongEnough(input)) {
console.log("Password is long enough");
}

if (!isLongEnough(input)) {
console.log("Password isn't long enough");
}

if (hasSpecialCharacter(input)) {
console.log("Password contains a special character");
}

if (!hasSpecialCharacter(input)) {
console.log("Password contains no special character");
}

}

isPasswordValid("passsword%");


#4

we discuss in that thread what you should do? Did you check documentation about .search() on MDN?


#5

You did but you talk about .search and regex which I don't understand. And I've just had a look at MDN but it still doesn't make it much clearer for me because It's talking about regular expressions which I haven't learnt. And then I go on the page about regular expressions and it's talking about flags etc which I also haven't learnt.

On the other thread you even agreed with cilariel that this stuff isn't for beginners.

So in this case, what do I have to write, where am I supposed to put it and why does it make it work? Because I know nothing about .search + regex and this course doesn't cover any of the concepts that MDN is talking about.

But all this brings me back to my original point. If you follow the step by step instructions for this project, it doesn't work correctly. I would have to go away and learn about things that are not covered in this course in order to make it work. And then I would have to ignore the step by step and do it my own way. Therefor it's designed poorly?

And thanks for your help :slight_smile: Sorry if I come across as being arsey, I just don't understand this stuff and the course doesn't cover it :stuck_out_tongue:


#6

Anyway, you can simply do:

function hasUppercase(input) {
   if (input.search(/[A-Z]/) == 1){ return true } else {return false}
}

search will use a regular expression to find a till z.

I agree, there is one flaw in the project, doesn't make the whole project bad


#7

Sorry I didn't mean it like that. The way I put it sounded too harsh for what it actually is. I changed the thread title to what I actually meant. Anyway I'm going to move on, even when using your code, it still doesn't work right.

Here's my code if you want to try and figure it out, I'm sure I've gone wrong somewhere. Thanks for your help anyway

function hasUppercase(input) {
   if (input.search(/[A-Z]/) == 1){ 
     return true; 
   } else {
     return false;
   }
}

function hasLowerCase(input) {
   if (input.search(/[a-z]/) == 1){ 
     return true; 
   } else {
     return false;
   }
}

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;
      }
    }
  }
}

function isPasswordValid(input) {
  if (hasUppercase(input)) {
    console.log("Password contains an uppercase letter");
  }
  
  if (!hasUppercase(input)) {
    console.log("The Password contains no uppercase letters");
  }
  
  if (hasLowerCase(input)) {
    console.log("Password contains a lowercase letter");
  }
  
  if (!hasLowerCase(input)) {
    console.log("Password contains no lowercase letters");
  }

  if (isLongEnough(input)) {
    console.log("Password is long enough");
  }
  
  if (!isLongEnough(input)) {
    console.log("Password isn't long enough");
  }
  
  if (hasSpecialCharacter(input)) {
    console.log("Password contains a special character");
  }
  
  if (!hasSpecialCharacter(input)) {
    console.log("Password contains no special character");
  }
  
}


isPasswordValid("PASSWORD1%");

try passwords such as 'aPassword1%', 'Apassword1%', '00Password1%', 'PASSWORD1%', 'password1%' and you'll see what I mean.


#8

oh danm, search returns 0 if a match is found. Okay, in that case here is your code in a bin. Now it should work


#9

It works for 'PASSWORD1%' now, but if you change it to 'pASSWORD1%' it won't read any of the uppercase. If you put '00Password1%', it won't read any of the uppercase or lowercase.

It's as if it's not reading all the way through the string for some reason.


#11

If-statements aren't required to evaluate boolean expressions, please return the result instead.
a-z aren't the only cased letters, string method would be better

Generally we need to argue for why something would work. If using a function/method, then we need to know how it behaves. We can know that either because it promises to follow some convention that we're familiar with, or by looking up its documentation.


#12

Hello.

I found the solution to this issue with the uppercase and lowercase not being validated properly. I don't think a regex is required for this problem. I found the solution by using the methods toLowerCase and toUpperCase from a post on stack overflow:

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

function hasLowercase(input) {
  if (input.toUpperCase() != input) {
    return true;
  }
  return false;
}

You can see the entire solution here.


#13

I am dealing with the exact same problems. Thank you for this thread.


#14