1/*----------------------------------------------------------------------*/ 2/* wl_Valid 3/* description: validates an input 4/* dependency: 5/*----------------------------------------------------------------------*/ 6 7$.fn.wl_Valid = function (method) { 8 9 var args = arguments; 10 11 return this.each(function () { 12 13 var $this = $(this); 14 15 if ($.fn.wl_Valid.methods[method]) { 16 return $.fn.wl_Valid.methods[method].apply(this, Array.prototype.slice.call(args, 1)); 17 } else if (typeof method === 'object' || !method) { 18 if ($this.data('wl_Valid')) { 19 var opts = $.extend({}, $this.data('wl_Valid'), method); 20 } else { 21 var opts = $.extend({}, $.fn.wl_Valid.defaults, method, $this.data()); 22 } 23 } else { 24 $.error('Method "' + method + '" does not exist'); 25 } 26 27 //regex is a string => convert it 28 if (typeof opts.regex === 'string') { 29 opts.regex = new RegExp(opts.regex); 30 } 31 32 if (!$this.data('wl_Valid')) { 33 34 $this.data('wl_Valid', {}); 35 36 $this.bind({ 37 38 //validate on change event 39 'change.wl_Valid': function () { 40 var opts = $this.data('wl_Valid') || opts; 41 $.fn.wl_Valid.methods.validate.call($this[0]); 42 43 //callback 44 opts.onChange.call($this[0], $this, $this.val()); 45 if (!opts.valid) { 46 //error callback 47 opts.onError.call($this[0], $this, $this.val()); 48 } 49 }, 50 51 //for instant callback 52 'keyup.wl_Valid': function () { 53 var opts = $this.data('wl_Valid') || opts; 54 if (opts.instant) { 55 //validate only if minlength is reached 56 if ($this.val().length >= $this.data('wl_Valid').minLength) { 57 $this.wl_Valid('validate'); 58 } 59 } 60 } 61 }); 62 63 } else { 64 65 } 66 67 if (opts) $.extend($this.data('wl_Valid'), opts); 68 $.fn.wl_Valid.methods.validate.call($this); 69 }); 70 71}; 72 73$.fn.wl_Valid.defaults = { 74 errorClass: 'error', 75 instant: true, 76 regex: /.*/, 77 minLength: 0, 78 onChange: function ($this, value) {}, 79 onError: function ($this, value) {} 80}; 81 82$.fn.wl_Valid.version = '1.0'; 83 84 85$.fn.wl_Valid.methods = { 86 validate: function () { 87 var $this = $(this), 88 opts = $this.data('wl_Valid') || opts, 89 value = $this.val(); 90 if (typeof opts.customValidation == 'function') { 91 opts.valid = opts.customValidation(value); 92 if (opts.valid) { 93 $this.removeClass(opts.errorClass); 94 } else { 95 $this.addClass(opts.errorClass); 96 } 97 } else { 98 //check for validation, empty is valid too! 99 opts.valid = (!value || opts.regex.test(value)); 100 101 //field is valid or equal to a placeholder attribute 102 if (opts.valid || (value == $this.attr('placeholder'))) { 103 $this.removeClass(opts.errorClass); 104 } else { 105 $this.addClass(opts.errorClass); 106 } 107 } 108 }, 109 set: function () { 110 var $this = $(this), 111 options = {}; 112 if (typeof arguments[0] === 'object') { 113 options = arguments[0]; 114 } else if (arguments[0] && arguments[1] !== undefined) { 115 options[arguments[0]] = arguments[1]; 116 } 117 $.each(options, function (key, value) { 118 if ($.fn.wl_Valid.defaults[key] !== undefined || $.fn.wl_Valid.defaults[key] == null) { 119 switch (key) { 120 case 'regex': 121 value = new RegExp(value); 122 break; 123 default: 124 } 125 $this.data('wl_Valid')[key] = value; 126 } else { 127 $.error('Key "' + key + '" is not defined'); 128 } 129 }); 130 131 } 132}; 133 134 135 136/*----------------------------------------------------------------------*/ 137/* wl_Mail 138/* description: Shorthand for wl_Valid for email addresses 139/* dependency: wl_Valid 140/*----------------------------------------------------------------------*/ 141 142$.fn.wl_Mail = function (method) { 143 144 var args = arguments; 145 return this.each(function () { 146 147 var $this = $(this); 148 if ($.fn.wl_Valid.methods[method]) { 149 return $.fn.wl_Valid.methods[method].apply(this, Array.prototype.slice.call(args, 1)); 150 } else if (typeof method === 'object' || !method) { 151 if ($this.data('wl_Valid')) { 152 var opts = $.extend({}, $this.data('wl_Valid'), method); 153 } else { 154 var opts = $.extend({}, $.fn.wl_Mail.defaults, method, $this.data()); 155 } 156 } else { 157 $.error('Method "' + method + '" does not exist'); 158 } 159 160 $this.wl_Valid(opts); 161 162 if (opts) $.extend($this.data('wl_Valid'), opts); 163 164 }); 165 166}; 167 168$.fn.wl_Mail.defaults = { 169 regex: /^([\w-]+(?:\.[\w-]+)*)\@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?(\d{1,3}\.){3}\d{1,3}\]?)$/i, 170 onChange: function (element, value) { 171 element.val(value.toLowerCase()); 172 } 173}; 174 175/*----------------------------------------------------------------------*/ 176/* wl_URL 177/* description: Shorthand for wl_Valid for urls
178/* dependency: wl_Valid 179/*----------------------------------------------------------------------*/ 180 181$.fn.wl_URL = function (method) { 182 183 var args = arguments; 184 return this.each(function () { 185 186 var $this = $(this); 187 188 if ($.fn.wl_Valid.methods[method]) { 189 return $.fn.wl_Valid.methods[method].apply(this, Array.prototype.slice.call(args, 1)); 190 } else if (typeof method === 'object' || !method) { 191 if ($this.data('wl_Valid')) { 192 var opts = $.extend({}, $this.data('wl_Valid'), method); 193 } else { 194 var opts = $.extend({}, $.fn.wl_URL.defaults, method, $this.data()); 195 } 196 } else { 197 $.error('Method "' + method + '" does not exist'); 198 } 199 200 $this.wl_Valid(opts); 201 202 if (opts) $.extend($this.data('wl_Valid'), opts); 203 204 }); 205 206}; 207 208$.fn.wl_URL.defaults = { 209 regex: /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w]))*\.+(([\w#!:.?+=&%@!\-\/]))?/, 210 instant: false, 211 onChange: function (element, value) { 212 if (value != '' && !/^(ftp|http|https):\/\//.test(value)) element.val('http://' + value).trigger('change.wl_Valid'); 213 } 214};
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.