I have 4 requirements for user passwords:
.
You can use following expression:
/[a-z]/.test(pass)+/[A-Z]/.test(pass)+/\d/.test(pass)+/[.!@#$%^&*()_]/.test(pass)>2
Always break down big problems into smaller ones.
Define a separate Regex for each of your four different conditions, then check if enough of them are fulfilled.
For example:
var checks = {
lowercase: /[a-z]/,
uppercase: /[A-Z]/,
number: /[0-9]/,
symbol: /[.!@#$%^&*()_]/
}, passcount = 0, results = {};
for( var k in checks) if( checks.hasOwnProperty(k)) {
if( checks[k].test(password)) {
passcount++;
results[k] = true;
}
else results[k] = false;
}
if( passcount < 2) {
alert("Your password didn't meet enough conditions.\n" +
"[Provide useful info here - 'results' object lists " +
"whether each test passed or failed, so use that for " +
"a user-friendly experience!]");
return false;
}
return true;
And finally, obligatory xkcd comic: