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