I am trying to test passwords. I need to do some testing on the string. I am finding that my special characters evaluate to true when I test them with toUpperCase(). I would suspect that they would evaluate to false.
How should I test for special characters?
// CREATE ARRAY OF CHARACTERS
CharacterArray = ["1","2","3","a","b","c","D","E","F","/","+",")"];
// TEST CHARACTERS
function testCharacters() {
// GET ARRAY LEN
var CharacterArrayLen = CharacterArray.length;
// LOOP THROUGH ARRAY
for (i = 0; i < CharacterArrayLen; i++) {
// PARSE SINGLE CHARACTER
var ThisChar = CharacterArray[i];
if (!isNaN(ThisChar)) {
alert(ThisChar + ' is numeric!');
} else {
if (ThisChar === ThisChar.toUpperCase()) {
alert(ThisChar + ' is upper case');
} else if (ThisChar === ThisChar.toLowerCase()) {
alert(ThisChar + " is lower case");
}
}
}
}
testCharacters();
They aren’t upper or lower case. Both
toUpperCaseandtoLowerCasereturn the same thing for those characters. If you tested for lower case first, you’d find they’re all lower case!You can use some regex to see if the character is in the alphabet.
will return
nullon non-alphabet characters.If you’re just trying to see if each of those characters is in your array, you can use
indexOfThe second one returns
-1becauseAis not in the array.Here’s a DEMO using regex.