ShiroHaru __ ShiroHaru __ - 10 days ago 6
Javascript Question

Password strength regex with numbers

I'm working on a strength password test, and I'm trying to check if the password has upper, and lower, case characters. I'm using two regular expressions, and they're almost working; with the code just here:



var upper = false;
var lower = false;
var upperCase= new RegExp('[^A-Z]');
var lowerCase= new RegExp('[^a-z]');



if (password.match(upperCase )){
upper = true;
}
if (password.match(lowerCase)){
lower = true;
}





When I'm typing numbers, or just a digit, like "1", upper and lower become true.

I'm not really good with regex, did I made a mistake?

Answer

Take a look at the following which uses test to return a Boolean of whether the password contains an upper case and a lower case. You will see how it tests both regexes against various passwords.

    var upperCase= new RegExp('[A-Z]');
    var lowerCase= new RegExp('[a-z]');
    

function test(password) {
    return [ upperCase.test(password), lowerCase.test(password)];
  }

console.log(test("Abc"));
console.log(test("abc"));
console.log(test("123"));