mangesh
mangesh

Reputation: 365

Regex not working - min 8 character, 1 Number, 1 special character and 1 uppercase letter

I want a regular expression to check that checks for a minimum of 8 characters with 1 number, 1 special character and 1 uppercase letter.

function checkPassword(str) {
  var re = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})$/;
  return re.test(str);
}

Upvotes: 0

Views: 725

Answers (1)

Chase
Chase

Reputation: 3105

KISS - keep it simple.
Is your string length 8 or more? No? Test failed. Yes? Proceed.
Run 4 regex tests on string, one for each parameter.
4 truthy values means you're good to go. Any falsey values means no validation.
Complicated regex will always bite you with weird edge cases if you do not understand or test it thoroughly. Break it into smaller understandable pieces and run the tests consecutively. You want to be correct, not stylish.

Upvotes: 2

Related Questions