1/*----------------------------------------------------------------------*/ 2/* wl_Password v 1.0 by revaxarts.com 3/* description: handles password fields 4/* dependency: none 5/*----------------------------------------------------------------------*/ 6$.fn.wl_Password = function (method) { 7 8 var args = arguments; 9 return this.each(function () { 10 11 var $this = $(this); 12 13 if ($this.data('wl_Password') && $this.data('wl_Password').confirmfield) { 14 return; 15 } 16 if ($.fn.wl_Password.methods[method]) { 17 return $.fn.wl_Password.methods[method].apply(this, Array.prototype.slice.call(args, 1)); 18 } else if (typeof method === 'object' || !method) { 19 if ($this.data('wl_Password')) { 20 var opts = $.extend({}, $this.data('wl_Password'), method); 21 } else { 22 var opts = $.extend({}, $.fn.wl_Password.defaults, method, $this.data()); 23 } 24 } else { 25 $.error('Method "' + method + '" does not exist'); 26 } 27 28 29 if (!$this.data('wl_Password')) { 30 31 $this.data('wl_Password', {}); 32 33 34 //The strnghtmeter 35 var $strength = $('<div/>', { 36 'class': 'passwordstrength' 37 }).appendTo($this.parent()).hide(); 38 39 //if confirm field 40 if (opts.confirm) { 41 //clone the password field and append it after the field or after the strengthmeter. Hide it 42 opts.connect = this.id + '_confirm'; 43 var $confirm = $($this.addClass('password').clone()).attr({ 44 'id': opts.connect 45 }).appendTo($this.parent()).removeAttr('required').hide(); 46 $confirm.data('wl_Password', { 47 confirmfield: true 48 }); 49 } 50 51 52 $this.bind({ 53 54 //focus triggers a keyup 55 'focus.wl_Password': function () { 56 $this.trigger('keyup.wl_Password'); 57 }, 58 59 //blur sets the strengthmeter 60 'blur.wl_Password': function () { 61 var opts = $this.data('wl_Password') || opts; 62 if ($this.val()) { 63 if (opts.confirm && !$confirm.val()) $strength.text(opts.text.confirm); 64 } else { 65 $strength.hide(); 66 if (opts.confirm) $confirm.hide(); 67 } 68 }, 69 70 'keyup.wl_Password': function () { 71 var opts = $this.data('wl_Password') || opts; 72 73 //if value is set 74 if ($this.val()) { 75 76 //show optional confirm field 77 if (opts.confirm) $confirm.show(); 78 79 //get the strength of the current value 80 var _strength = getStrength($this.val(), opts.minLength); 81 82 //show optional strengthmeter 83 if (opts.showStrength) { 84 $strength.show(); 85 $strength.attr('class', 'passwordstrength').addClass('s_' + _strength).text(opts.words[_strength]); 86 } 87 88 //add strength to the DOM element 89 $this.data('wl_Password').strength = _strength; 90 91 //hide siblings if no value is set 92 } else { 93 if (opts.showStrength) $strength.hide(); 94 if (opts.confirm) $confirm.val('').hide(); 95 } 96 } 97 }); 98 99 //bind only when confirmation and strengthmeter is active 100 if (opts.confirm && opts.showStrength) { 101 $confirm.bind('keyup.wl_Password', function () { 102 var opts = $this.data('wl_Password') || opts; 103 if (!$confirm.val()) { 104 //confirm text 105 $strength.text(opts.text.confirm); 106 } else if ($confirm.val() != $this.val()) { 107 //password doesn't match 108 $strength.text(opts.text.nomatch); 109 } else { 110 //password match => show strength 111 $strength.text(opts.words[$this.data('wl_Password').strength]); 112 } 113 }); 114 } 115 116 //calculates the strenght of a password 117 //must return a value between 0 and 5 118 //value is the password 119 function getStrength(value, minLength) { 120 var score = 0; 121 if (value.length < minLength) { 122 return score 123 } else { 124 score = Math.min(15, (score + (value.length) * 2)); 125 } 126 if (value.match(/[a-z]/)) score += 1; 127 if (value.match(/[A-Z]/)) score += 5; 128 if (value.match(/\d+/)) score += 5; 129 if (value.match(/(.*[0-9].*[0-9].*[0-9])/)) score += 7; 130 if (value.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)) score += 5; 131 if (value.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 7; 132 if (value.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) score += 2; 133 if (value.match(/([a-zA-Z])/) && value.match(/([0-9])/)) score
133+= 3; 134 if (value.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)) score += 3; 135 return Math.min(5, Math.ceil(score / 10)); 136 } 137 138 } else { 139 140 } 141 142 if (opts) $.extend($this.data('wl_Password'), opts); 143 }); 144 145}; 146 147$.fn.wl_Password.defaults = { 148 confirm: true, 149 showStrength: true, 150 words: ['too short', 'bad', 'medium', 'good', 'very good', 'excellent'], 151 minLength: 3, 152 text: { 153 confirm: 'please confirm', 154 nomatch: 'password doesn\'t match' 155 } 156}; 157 158$.fn.wl_Password.version = '1.0'; 159 160$.fn.wl_Password.methods = { 161 set: function () { 162 var $this = $(this), 163 options = {}; 164 if (typeof arguments[0] === 'object') { 165 options = arguments[0]; 166 } else if (arguments[0] && arguments[1] !== undefined) { 167 options[arguments[0]] = arguments[1]; 168 } 169 $.each(options, function (key, value) { 170 if ($.fn.wl_Password.defaults[key] !== undefined || $.fn.wl_Password.defaults[key] == null) { 171 $this.data('wl_Password')[key] = value; 172 } else { 173 $.error('Key "' + key + '" is not defined'); 174 } 175 }); 176 177 } 178};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.