Last active
July 19, 2016 19:43
-
-
Save danrichards/878d2727938081bb3c24e0a6ff51b916 to your computer and use it in GitHub Desktop.
Add a password strength meter to your sign up forms.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ( $ ) { | |
$.fn.addPasswordStrengthMeter = function(meter_el) { | |
var that = this; | |
if (typeof meter_el == 'undefined') { | |
meter_el = $('.password-strength-meter'); | |
} | |
var bars = meter_el.children(); | |
if (bars.length != 4) { | |
console.log("Exception: $.fn.passwordStrength() must reference a meter with four children."); | |
return; | |
} | |
var assertions = [ | |
// Two uppercase. | |
function (value) { | |
var matches = value.match(/^.*[A-Z].*[A-Z].*$/); | |
return value.length >= 8 && matches != null | |
}, | |
// Two digits. | |
function (value) { | |
var matches = value.match(/^.*[0-9].*[0-9].*$/); | |
return value.length >= 8 && matches != null | |
}, | |
// One special case letter. | |
function (value) { | |
var matches = value.match(/^.*[!@#%^&*\$].*$/); | |
return value.length >= 8 && matches != null | |
}, | |
// Three lowercase. | |
function (value) { | |
var matches = value.match(/^.*[a-z].*[a-z].*[a-z].*$/); | |
return value.length >= 8 && matches != null | |
} | |
]; | |
this.on('keyup', function() { | |
var value = that.val(); | |
var strength = 0; | |
$.each(assertions, function(index, assertion) { | |
if (assertion(value)) { | |
strength++; | |
} | |
}); | |
var counter = 0; | |
for (var i = 3; i >= 0; i--) { | |
if (strength > counter) { | |
$(bars.get(i)).addClass('active'); | |
} else { | |
$(bars.get(i)).removeClass('active'); | |
} | |
counter ++; | |
} | |
}); | |
return this; | |
}; | |
}( jQuery )); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="form-group"> | |
<label for="signup-password">Password</label> | |
<input type="password" class="form-control" id="signup-password" name="password"> | |
<div class="password-strength-meter landing"> | |
<span></span> | |
<span></span> | |
<span></span> | |
<span></span> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment