Skip to content

Instantly share code, notes, and snippets.

@FSX
Created February 25, 2011 18:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FSX/844270 to your computer and use it in GitHub Desktop.
Save FSX/844270 to your computer and use it in GitHub Desktop.
A password strength checker plugin for jQuery
// Usage: $('input.pwstrnth').pwstrnth();
// Password strength checker plugin
;(function ($, window, undefined) {
var pluginName = 'pwstrnth',
document = window.document,
indicators = [' ', ':\'(', ':(', ':|', ':)', ':D'],
regexes = [/.{8,}/, /[a-z]+/, /[0-9]+/, /[A-Z]+/, /[\/?<>,.\[\]{}()*&^%$#@!;:|]/];
function Plugin(element) {
this.$element = $(element);
this.init();
};
Plugin.prototype = {
init: function() {
var self = this;
this.id = this.$element.attr('id') || this.$element.attr('name');
this.$indicatorBox = $('<span id="' + this.id + '-pwstrnth" class="pwstrnth-indicator">&nbsp;</span>');
this.$element.after(this.$indicatorBox);
this.indicator();
this.$element.keyup(function() {
self.indicator();
});
},
indicator: function() {
var value = this.$element.attr('value');
if (this.$element.attr('value').length > 0) {
this.$indicatorBox.text(indicators[this.strength(value)]);
if (!this.$indicatorBox.is(':visible')) {
this.$indicatorBox.fadeIn();
}
} else {
this.$indicatorBox.fadeOut();
}
},
strength: function(value) {
var strength = 0;
jQuery.each(regexes, function(i, regex) {
if (value.match(regex)) {
strength++;
}
});
return strength
}
};
$.fn[pluginName] = function () {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new Plugin(this));
}
});
}
}(jQuery, window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment