Skip to content

Instantly share code, notes, and snippets.

@influxweb
Created June 19, 2013 15:29
Show Gist options
  • Save influxweb/5815231 to your computer and use it in GitHub Desktop.
Save influxweb/5815231 to your computer and use it in GitHub Desktop.
Test Password Strength When building forms, it’s a very good practice to provide verifications on the front-end first so the visitor do not have to submit the form endlessly to correct problems. This code snippet is using regular expressions to test if a password is strong enough. Of course, don’t forget to validate your forms on the server side…
$('#pass').keyup(function(e) {
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
if (false == enoughRegex.test($(this).val())) {
$('#passstrength').html('More Characters');
} else if (strongRegex.test($(this).val())) {
$('#passstrength').className = 'ok';
$('#passstrength').html('Strong!');
} else if (mediumRegex.test($(this).val())) {
$('#passstrength').className = 'alert';
$('#passstrength').html('Medium!');
} else {
$('#passstrength').className = 'error';
$('#passstrength').html('Weak!');
}
return true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment