-2

I'm trying to create a password validation by checking if it has lower- and uppercase letters as well as special characters but still fail to do it.
So I have:

testPwd(val){
  return Boolean(/^[a-zA-Z0-9]+$/.test(val));
}

The following test proves that the reges does not work properly:

console.log(testPwd("123")  

As it shows true.
Could you provide the valid regex, to make sure that all scenarios are included (upper and lower case letters plus special characters)?

Vitalii Isaenko
  • 941
  • 1
  • 14
  • 37
Geoff
  • 6,277
  • 23
  • 87
  • 197

3 Answers3

2

While @kltr's answer is correct I do not recommend the use of such verbose regex (especially if you are not very familiar with them). Considering it's client code and not a function that will be executed in large volumes by a backend service you don't need to worry so much about performance so my suggestion would be to make a regex check for each of your required chars separately, this would allow the code to be more understandable and easier to edit in the future, in case you will want to add or remove certain required chars.

testPwd = function(val) {
    var lowercase = /[a-z]/.test(val);
    var uppercase = /[A-Z]/.test(val);
    var numbers = /[0-9]/.test(val);
    return lowercase === true && uppercase === true && numbers === true;    
}

you can choose how verbose you want it to be (for example not assigning a var to each regex, etc) or adapt each regex to your linking but IMO i would go with this one. This would also allow you to provide a verbose error response (Example: "You are missing an Uppercase letter") with relatively little effort.

Marco Yammine
  • 323
  • 1
  • 10
2

This works:

function testPwd(val) {
  return Boolean(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%]).{4,8}$/.test(val));
}

console.log(testPwd('1dDs!'));

It also has optional password length check, too. So, the regex checks for passwords between 4-8 characters, which you can change as you wish or remove, and also checks for:

  • 1 or more uppercase
  • 1 or more lowercase
  • 1 or more special character in [!@#$%]

Working demo: https://jsfiddle.net/16bzfLj3/5/

Martin Geldart
  • 407
  • 2
  • 8
0

Your code does not work correctly because:

  1. You did not include the check for symbols
  2. It is simply checking whether any of those conditions are met, not all.

Some answers have already covered the correct way to do your checking. While they are good, they lack explanation.

So here's a simplified one that explains each line to you:

function testPwd(str){
  var lowerCase = /[a-z]/,      // test for lowercase only
      upperCase = /[A-Z]/,      // test for uppercase only
      number = /[0-9]/,         // test for number only
      symbols = /[^a-zA-Z0-9]/; // test for non-string and non-number

  // If there isn't any lower case, return false
  if (lowerCase.test(str) === false)
    return false;
 
  // If there isn't any uppercase case, return false
  if (upperCase.test(str) === false)
    return false;
    
  // If there isn't any number, return false
  if (number.test(str) === false)
    return false;
    
  // If there isn't any symbols, return false
  if (symbols.test(str) === false)
    return false;
    
  // Now if the code executes until this position,
  // it means all conditions are successfully met
  // so, return true;
  return true;
}

var testCase = ['1234', 'abcd', 'ABCD', '!@#$', '1bcD', '1bC$'];
testCase.forEach(function(pwd){
  console.log(pwd + ' : ' + testPwd(pwd));
});

Note that you should include length check in your actual code as well.

yqlim
  • 6,898
  • 3
  • 19
  • 43