| [1371] | 1 | /** |
|---|
| 2 | * jquery.pwstrength http://matoilic.github.com/jquery.pwstrength |
|---|
| 3 | * |
|---|
| 4 | * @version v0.1.1 |
|---|
| 5 | * @author Mato Ilic <info@matoilic.ch> |
|---|
| 6 | * @copyright 2013 Mato Ilic |
|---|
| 7 | * |
|---|
| 8 | * Dual licensed under the MIT and GPL licenses: |
|---|
| 9 | * http://www.opensource.org/licenses/mit-license.php |
|---|
| 10 | * http://www.gnu.org/licenses/gpl.html |
|---|
| 11 | */ |
|---|
| 12 | ;(function($) { |
|---|
| 13 | $.pwstrength = function(password) { |
|---|
| 14 | var score = 0, length = password.length, upperCase, lowerCase, digits, nonAlpha; |
|---|
| 15 | |
|---|
| 16 | if(length < 5) score += 0; |
|---|
| 17 | else if(length < 8) score += 5; |
|---|
| 18 | else if(length < 16) score += 10; |
|---|
| 19 | else score += 15; |
|---|
| 20 | |
|---|
| 21 | lowerCase = password.match(/[a-z]/g); |
|---|
| 22 | if(lowerCase) score += 1; |
|---|
| 23 | |
|---|
| 24 | upperCase = password.match(/[A-Z]/g); |
|---|
| 25 | if(upperCase) score += 5; |
|---|
| 26 | |
|---|
| 27 | if(upperCase && lowerCase) score += 2; |
|---|
| 28 | |
|---|
| 29 | digits = password.match(/\d/g); |
|---|
| 30 | if(digits && digits.length > 1) score += 5; |
|---|
| 31 | |
|---|
| 32 | nonAlpha = password.match(/\W/g) |
|---|
| 33 | if(nonAlpha) score += (nonAlpha.length > 1) ? 15 : 10; |
|---|
| 34 | |
|---|
| 35 | if(upperCase && lowerCase && digits && nonAlpha) score += 15; |
|---|
| 36 | |
|---|
| 37 | if(password.match(/\s/)) score += 10; |
|---|
| 38 | |
|---|
| 39 | if(score < 15) return 0; |
|---|
| 40 | if(score < 20) return 1; |
|---|
| 41 | if(score < 35) return 2; |
|---|
| 42 | if(score < 50) return 3; |
|---|
| 43 | return 4; |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | function updateIndicator(event) { |
|---|
| 47 | var strength = $.pwstrength($(this).val()), options = event.data, klass; |
|---|
| 48 | klass = options.classes[strength]; |
|---|
| 49 | |
|---|
| 50 | options.indicator.removeClass(options.indicator.data('pwclass')); |
|---|
| 51 | options.indicator.data('pwclass', klass); |
|---|
| 52 | options.indicator.addClass(klass); |
|---|
| 53 | options.indicator.find(options.label).html(options.texts[strength]); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | $.fn.pwstrength = function(options) { |
|---|
| 57 | var options = $.extend({ |
|---|
| 58 | label: '.label', |
|---|
| 59 | classes: ['pw-very-weak', 'pw-weak', 'pw-mediocre', 'pw-strong', 'pw-very-strong'], |
|---|
| 60 | texts: ['very weak', 'weak', 'mediocre', 'strong', 'very strong'] |
|---|
| 61 | }, options || {}); |
|---|
| 62 | options.indicator = $('#' + this.data('indicator')); |
|---|
| 63 | |
|---|
| 64 | return this.keyup(options, updateIndicator); |
|---|
| 65 | }; |
|---|
| 66 | })(jQuery); |
|---|