Skip to content

Instantly share code, notes, and snippets.

@abrahamfast
Last active November 11, 2016 08:16
Show Gist options
  • Save abrahamfast/4c53d654461e2e9fea58821cb532b0fd to your computer and use it in GitHub Desktop.
Save abrahamfast/4c53d654461e2e9fea58821cb532b0fd to your computer and use it in GitHub Desktop.
perfect password regex in js
var bad = /(?=.{8,}).*/;
//Alpha Numeric plus minimum 8
var good = /^(?=\S*?[a-z])(?=\S*?[0-9])\S{8,}$/;
//Must contain at least one upper case letter, one lower case letter and (one number OR one special char).
var better = /^(?=\S*?[A-Z])(?=\S*?[a-z])((?=\S*?[0-9])|(?=\S*?[^\w\*]))\S{8,}$/;
//Must contain at least one upper case letter, one lower case letter and (one number AND one special char).
var best = /^(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])(?=\S*?[^\w\*])\S{8,}$/;
$('#password').on('keyup', function () {
var password = $(this);
var pass = password.val();
var passLabel = $('[for="password"]');
var stength = 'Weak';
var pclass = 'danger';
if (best.test(pass) == true) {
stength = 'Very Strong';
pclass = 'success';
} else if (better.test(pass) == true) {
stength = 'Strong';
pclass = 'warning';
} else if (good.test(pass) == true) {
stength = 'Almost Strong';
pclass = 'warning';
} else if (bad.test(pass) == true) {
stength = 'Weak';
} else {
stength = 'Very Weak';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment