﻿window.digitalspaghetti = window.digitalspaghetti || {}; digitalspaghetti.password = { 'defaults': { 'displayMinChar': true, 'minChar': 8, 'minCharText': 'You must enter a minimum of %d characters', 'colors': ["#f00", "#c06", "#f60", "#3c0", "#3f0"], 'scores': [20, 30, 43, 50], 'verdicts': ['Weak', 'Normal', 'Medium', 'Strong', 'Very Strong'], 'raisePower': 1.4, 'width': 100, 'display': 'inline', 'prefix': 'Password secure:' }, 'ruleScores': { 'length': 0, 'lowercase': 1, 'uppercase': 3, 'one_number': 3, 'three_numbers': 5, 'one_special_char': 3, 'two_special_char': 5, 'upper_lower_combo': 2, 'letter_number_combo': 2, 'letter_number_char_combo': 2 }, 'rules': { 'length': true, 'lowercase': true, 'uppercase': true, 'one_number': true, 'three_numbers': true, 'one_special_char': true, 'two_special_char': true, 'upper_lower_combo': true, 'letter_number_combo': true, 'letter_number_char_combo': true }, 'validationRules': { 'length': function (word, score) {    var wordlen = word.length; var lenScore = Math.pow(wordlen, 1.4); if (wordlen < digitalspaghetti.password.options.minChar) { lenScore = (lenScore - 100); }    return lenScore;}, 'lowercase': function (word, score) { return word.match(/[a-z]/) && score; }, 'uppercase': function (word, score) { return word.match(/[A-Z]/) && score; }, 'one_number': function (word, score) { return word.match(/\d+/) && score; }, 'three_numbers': function (word, score) { return word.match(/(.*[0-9].*[0-9].*[0-9])/) && score; }, 'one_special_char': function (word, score) { return word.match(/.[!,@,#,$,%,\^,&,*,?,_,~]/) && score; }, 'two_special_char': function (word, score) { return word.match(/(.*[!,@,#,$,%,\^,&,*,?,_,~].*[!,@,#,$,%,\^,&,*,?,_,~])/) && score; }, 'upper_lower_combo': function (word, score) { return word.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/) && score; }, 'letter_number_combo': function (word, score) { return word.match(/([a-zA-Z])/) && word.match(/([0-9])/) && score; }, 'letter_number_char_combo': function (word, score) { return word.match(/([a-zA-Z0-9].*[!,@,#,$,%,\^,&,*,?,_,~])|([!,@,#,$,%,\^,&,*,?,_,~].*[a-zA-Z0-9])/) && score; } }, 'attachWidget': function (element, options) {    var output = ['<div id="password-strength" style="display:inline">']; if (options.displayMinChar) { output.push('<span class="password-min-char">' + options.minCharText.replace('%d', options.minChar) + '</span>'); }    output.push('<span class="password-strength-bar"></span>'); output.push('</div>'); output = output.join(''); if (options.passwContainer) jQuery("#" + options.passwContainer).append(output); else jQuery(element).after(output);}, 'addRule': function (name, method, score, active) { digitalspaghetti.password.rules[name] = active; digitalspaghetti.password.ruleScores[name] = score; digitalspaghetti.password.validationRules[name] = method; }, 'init': function (element, options) { digitalspaghetti.password.options = digitalspaghetti.password.defaults; var options = jQuery.extend({}, digitalspaghetti.password.defaults, options); digitalspaghetti.password.attachWidget(element, options); jQuery(element).keyup(function () { digitalspaghetti.password.calculateScore(jQuery(this).val(), options); }); }, 'calculateScore': function (word, options) { digitalspaghetti.password.totalscore = 0; for (var key in digitalspaghetti.password.rules) if (digitalspaghetti.password.rules.hasOwnProperty(key)) { if (digitalspaghetti.password.rules[key] === true) { var score = digitalspaghetti.password.ruleScores[key]; var result = digitalspaghetti.password.validationRules[key](word, score); if (result) { digitalspaghetti.password.totalscore += result; } } if (digitalspaghetti.password.totalscore === -200) { digitalspaghetti.password.strColor = '#f00'; digitalspaghetti.password.strText = options.verdicts[0]; } else if (digitalspaghetti.password.totalscore < 0 && digitalspaghetti.password.totalscore > -199) { digitalspaghetti.password.strColor = '#f00'; digitalspaghetti.password.strText = options.verdicts[0]; } else if (digitalspaghetti.password.totalscore <= digitalspaghetti.password.options.scores[0]) { digitalspaghetti.password.strColor = options.colors[0]; digitalspaghetti.password.strText = options.verdicts[0]; } else if (digitalspaghetti.password.totalscore > options.scores[0] && digitalspaghetti.password.totalscore <= options.scores[1]) { digitalspaghetti.password.strColor = options.colors[1]; digitalspaghetti.password.strText = options.verdicts[1]; } else if (digitalspaghetti.password.totalscore > options.scores[1] && digitalspaghetti.password.totalscore <= options.scores[2]) { digitalspaghetti.password.strColor = options.colors[2]; digitalspaghetti.password.strText = options.verdicts[2]; } else if (digitalspaghetti.password.totalscore > options.scores[2] && digitalspaghetti.password.totalscore <= options.scores[3]) { digitalspaghetti.password.strColor = options.colors[3]; digitalspaghetti.password.strText = options.verdicts[3]; } else { digitalspaghetti.password.strColor = options.colors[4]; digitalspaghetti.password.strText = options.verdicts[4]; } jQuery('#' + options.passwContainer + ' .password-strength-bar').stop(); jQuery('#' + options.passwContainer + ' .password-strength-bar').animate({ opacity: 0.5 }, 'fast', 'linear', function () { jQuery(this).css({ 'display': options.display, 'color': digitalspaghetti.password.strColor }).text(options.prefix + digitalspaghetti.password.strText); jQuery(this).animate({ opacity: 1 }, 'fast', 'linear'); }); } } }; jQuery.extend(jQuery.fn, { 'pstrength': function (options) { return this.each(function () { digitalspaghetti.password.init(this, options); }); } });
