Skip to content

Instantly share code, notes, and snippets.

@andrewstobbe
Created November 14, 2015 23:41
Show Gist options
  • Save andrewstobbe/507c506218b9e6bad5c6 to your computer and use it in GitHub Desktop.
Save andrewstobbe/507c506218b9e6bad5c6 to your computer and use it in GitHub Desktop.
$(document).ready(function () {
$('#password').keyup(function () {
$('#result').html(checkStrength($('#password').val()));
});
$('#password_confirmation').keyup(function () {
$('#confirm_result').html(checkMatch($('#password').val(), $('#password_confirmation').val()));
});
$('#result').html('Strength : Requires strong password.');
/**
* Check confirmation for match.
*
* @param password
* @param password_confirmation
*
* @returns {string}
*/
function checkMatch(password, password_confirmation) {
if (password === password_confirmation) {
return 'Match';
} else {
return 'Does Not Match';
}
}
/**
* Check the password strength.
*
* @param password
* @returns {string}
*/
function checkStrength(password) {
var strength = 0;
var message = 'Password is strong.';
var password_length = '8';
if (password.length < password_length) {
return 'Too Short : Requires '+password_length+' or more characters.';
}
if (password.length >= password_length) {
strength += 1;
}
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) {
strength += 1;
} else {
message = "Requires upper and lower case.";
}
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) {
strength += 1;
} else {
message = "Requires letters and numbers.";
}
if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/)) {
strength += 1;
} else {
message = "Requires Symbols (@, #, $, %, etc.).";
}
if (strength < 3) {
return 'Weak : ' + message;
} else if (strength == 3) {
return 'Moderate : ' + message;
} else {
return 'Acceptable : ' + message;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment