0

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?

David Thomas
  • 249,100
  • 51
  • 377
  • 410
ShiroHaru __
  • 103
  • 1
  • 1
  • 5

2 Answers2

0

Unless you are paid per line of code; you could simply write:

var upper = password.match(/[A-Z]/);
var lower = password.match(/[a-z]/);

This also fixes the bug in your code where you use [^] which negates the match (aka; your upperCase regexp match non-uppercase chars)

Sjon
  • 4,989
  • 6
  • 28
  • 46
0

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"));
Simon H
  • 20,332
  • 14
  • 71
  • 128