diff options
author | David Stutz <davidstutz@web.de> | 2013-10-21 00:06:14 +0200 |
---|---|---|
committer | David Stutz <davidstutz@web.de> | 2013-10-21 00:06:14 +0200 |
commit | 4485aaab1e468e3f050c8c75b3a0d1f604586e4f (patch) | |
tree | 545137243e004f38f6b3636c4a3755e6ad5778a2 /js/bootstrap-strength-meter.js | |
download | bootstrap-strength-meter-4485aaab1e468e3f050c8c75b3a0d1f604586e4f.zip bootstrap-strength-meter-4485aaab1e468e3f050c8c75b3a0d1f604586e4f.tar.gz bootstrap-strength-meter-4485aaab1e468e3f050c8c75b3a0d1f604586e4f.tar.bz2 |
Initial commit with README. Bootstrap strength meter is a password strength meter based on Password Score.
Diffstat (limited to 'js/bootstrap-strength-meter.js')
-rw-r--r-- | js/bootstrap-strength-meter.js | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/js/bootstrap-strength-meter.js b/js/bootstrap-strength-meter.js new file mode 100644 index 0000000..f0256d0 --- /dev/null +++ b/js/bootstrap-strength-meter.js @@ -0,0 +1,153 @@ +/** + * bootstrap-strength-meter.js + * https://github.com/davidstutz/bootstrap-strength-meter + * + * Copyright 2013 David Stutz + */ +!function($) { + + "use strict";// jshint ;_; + + var StrengthMeter = function(input, options) { + + var defaults = { + container: input.parent(), + base: 100, + hierarchy: { + '0': 'progress-bar-danger', + '50': 'progress-bar-warning', + '80': 'progress-bar-success' + }, + dictionaries: [ + + ], + keyboards: [ + + ] + }, + + settings = $.extend({}, defaults, options), + + template = '<div class="progress"><div class="progress-bar" role="progressbar" aria-valuemax="100" aria-valuemin="0" aria-valuenow="0"></div></div>', + + progress, + + progressBar, + + methods = { + + }, + + core = { + + /** + * Initialize the plugin. + */ + init: function() { + input.on('keyup', core.keyup); + progress = settings.container.append($(template)); + progressBar = $('.progress-bar', progress); + }, + + /** + * Update progress abr accordning + */ + update: function(value) { + var width = Math.floor(value/settings.base*100); + + if (width > 100) { + width = 100; + } + + progressBar + .attr('area-valuenow', width) + .css('width', width + '%'); + + for (var value in settings.hierarchy) { + if (width > value) { + progressBar + .removeClass() + .addClass('progress-bar') + .addClass(settings.hierarchy[value]); + } + } + }, + + /** + * Binding on keydown for updateing the progrssbar. + */ + keyup: function(event) { + var password = $(event.target).val() + var value = 0; + + if (password.length > 0) { + var score = new Score(password); + value = score.calculateEntropyScore(core.getScoreOptions()); + } + + core.update(value); + }, + + /** + * Get options for score. + */ + getScoreOptions: function() { + var options = []; + + for (var i = 0; i < settings.dictionaries.length; i++) { + options[options.length] = { + type: 'dictionary', + dictionary: settings.dictionaries[i] + }; + + options[options.length] = { + type: 'leet', + dictionary: settings.dictionaries[i] + }; + } + + for (var i = 0; i < settings.keyboards.length; i++) { + options[options.length] = { + type: 'keyboard', + dictionary: settings.keyboards[i] + }; + } + + options[options.length] = { + type: 'repitition' + }; + + options[options.length] = { + type: 'sequences' + }; + + options[options.length] = { + type: 'dates' + }; + + return options; + } + }; + + core.init(); + + return methods; + } + + $.fn.strengthMeter = function(options) { + var instance = this.data('strengthMeter'), + elem = this; + + return elem.each(function() { + var strengthMeter; + + if (instance) { + return; + } + + strengthMeter = new StrengthMeter(elem, options); + elem.data('strengthMeter', strengthMeter); + }); + }; + +}(window.jQuery); |