1jQuery.gvalidation = { 2 rules : { 3 required : /[^.*]/, 4 alpha : /^[a-z ._-]+$/i, 5 alphanum : /^[a-z0-9 ._-]+$/i, 6 digit : /^[-+]?[0-9]+$/, 7 nodigit : /^[^0-9]+$/, 8 nospace : /^[^ ]+$/, 9 number : /^[-+]?\d*\.?\d+$/, 10 email : /^([a-zA-Z0-9_\.\-\+%])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, 11 image : /.(jpg|jpeg|png|gif|bmp)$/i, 12 phone : /^\+{0,1}[0-9 \(\)\.\-]+$/, // alternate regex : /^[\d\s ().-]+$/,/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/ 13 phone_inter : /^\+{0,1}[0-9 \(\)\.\-]+$/, 14 url : /^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&%\$#\=~])*$/i 15 }, 16 errors : { 17 required : 'This field is required.', 18 alpha : 'This field accepts alphabetic characters only.', 19 alphanum : 'This field accepts alphanumeric characters only.', 20 digit : 'Please enter a valid integer.', 21 nodigit : 'No digits are accepted.', 22 nospace : 'No spaces are accepted.', 23 number : 'Please enter a valid number.', 24 email : 'Please enter a valid email.', 25 image : 'This field should only contain image types', 26 phone : 'Please enter a valid phone.', 27 phone_inter : 'Please enter a valid international phone number.', 28 url : 'Please enter a valid url.', 29 group: 'Please make at least %1 selection(s).', 30 confirm: 'Please make sure that the value matches the %1 field value.', 31 custom: 'The value entered is not valid.', 32 }, 33 display : 'tooltip', 34 'css':{ 35 'background-color':'#ff4242', 36 'border-color':'#ff0000', 37 'padding':'4px', 38 }, 39}; 40(function($){ 41 $.fn.gvalidate = function(options, params){ 42 if(this.length > 0){ 43 if($.type(params) === 'undefined' && $.type(options) === 'object'){ 44 params = options; 45 } 46 47 if($.type(options) === 'undefined' || $.type(options) === 'object'){ 48 params = $.extend({}, $.gvalidation, params); 49 return this.each(function(){ 50 if(!$(this).data('gvalidation')){ 51 $(this).data('gvalidation', new GValidation(this, params)); 52 } 53 }); 54 } 55 56 if($.type(options) === 'string'){ 57 params = $.extend({}, $.gvalidation, params); 58 var gval = $(this).data('gvalidation'); 59 60 if(typeof gval == 'undefined' || !gval){ 61 return null; 62 } 63 64 switch (options){ 65 case 'get': 66 return gval.get(); 67 } 68 } 69 } 70 } 71 72 var GValidation = function(elem, params){ 73 this.element = elem; 74 this.settings = params; 75 this.rules = []; 76 this.errors = {}; 77 this.invalid_rule = ''; 78 this.error_shown = false; 79 this.disabled = false; 80 81 this.init(); 82 }; 83 84 GValidation.prototype = { 85 init: function(){ 86 var gval = this; 87 if($(gval.element).is(':input')){ 88 gval.validate(false); 89 //validate the field when some events occur 90 $(gval.element).on('change', function(){ 91 gval.validate(true); 92 }); 93 $(gval.element).on('blur', function(){ 94 gval.validate(true); 95 }); 96 }else{ 97 //check on form submit 98 if($(gval.element).prop('tagName') == 'FORM'){ 99 /*$(gval.element).find(':input').on('blur', function(){ 100 $(this).gvalidate(); 101 }); 102 $(gval.element).find(':input').on('change', function(){ 103 $(this).gvalidate(); 104 });*/ 105 $(gval.element).find(':input').each(function(i, inp){ 106 $(inp).gvalidate(); 107 }); 108 $(gval.element).on('submit', function(e){ 109 //gval.init(); 110 $(gval.element).find(':input').each(function(i, inp){ 111 $(inp).gvalidate(); 112 }); 113 114 if(gval.validate_area() == true){ 115 $(gval.element).trigger('success.gvalidation'); 116 return true; 117 }else{ 118 e.stopImmediatePropagation(); 119 $(gval.element).trigger('fail.gvalidation'); 120 return false; 121 } 122 }); 123 $(gval.element).on('reset', function(e){ 124 setTimeout(function(){ 125 $(gval.element).find(':input').trigger('change'); 126 }, 500); 127 }); 128 }else{ 129 return gval.validate_area(); 130 } 131 } 132 }, 133 134 validate_area: function(){ 135 var gval = this; 136 var focus_gval = null; 137 $(gval.element).find(':input').each(function(i, inp){ 138 $(inp).gvalidate(); 139 if(focus_gval == null){ 140 var gval_inp = $(inp).gvalidate('get'); 141 if(!gval_inp.validate()){ 142 focus_gval = gval_inp; 143 return false;//break 144 } 145 } 146 }); 147 if(focus_gval != null){ 148 $(focus_gval.element).focus(); 149 $('html, body').animate({ 150 scrollTop: $(focus_gval.element).offset().top - 100 151 }, 300); 152 focus_gval.show_error(); 153 return false; 154 } 155 return true; 156 }, 157 158 get: function(){ 159 var gval = this; 160 161 return gval; 162 }, 163 164 reset: function(){ 165 var gval = this; 166 167 $(gval.element).data('gvalidation', null); 168 }, 169 170 validate: function(showError){ 171 var gval = this; 172 gval.inspect(); 173 174 var result = gval.check(); 175 if(result){ 176 if(gval.rule_name(gval.invalid_rule) == 'group'){ 177 $('input[class*="'+gval.invalid_rule+'"]').each(function(i, chk){ 178 var gval_other = $(chk).gvalidate('get'); 179 if(gval_other != null){ 180 gval_other.remove_error(); 181 } 182 });
183 }else if(gval.rules.length && gval.rule_name(gval.rules[0]) == 'group'){ 184 $('input[class*="'+gval.rules[0]+'"]').each(function(i, chk){ 185 var gval_other = $(chk).gvalidate('get'); 186 if(gval_other != null){ 187 gval_other.remove_error(); 188 } 189 }); 190 }else{ 191 gval.remove_error(); 192 } 193 }else{ 194 if(gval.rule_name(gval.invalid_rule) == 'group'){ 195 $('input[class*="'+gval.invalid_rule+'"]').each(function(i, chk){ 196 var gval_other = $(chk).gvalidate('get'); 197 if(gval_other != null){ 198 gval_other.check(); 199 } 200 }); 201 } 202 if(showError == true){ 203 gval.show_error(); 204 } 205 } 206 return result; 207 }, 208 209 inspect: function(){ 210 var gval = this; 211 var validate_matches = $(gval.element).prop('class').match(/validate\[(.*?)\]/g); 212 if(validate_matches){ 213 $.each(validate_matches, function(vm, validate_match){ 214 var matches = validate_match.match(/validate\[(.*?)\]/); 215 if(matches && typeof matches[1] != 'undefined'){ 216 var rules = matches[1].split(','); 217 var clean_rules = []; 218 $.each(rules, function(i, rule){ 219 rule = rule.replace(/("|')/g, ''); 220 clean_rules.push(rule); 221 }); 222 gval.set_rules(clean_rules); 223 } 224 }); 225 } 226 }, 227 228 check: function(){ 229 var gval = this; 230 var toReturn = true; 231 $(gval.element).trigger('check.gvalidation'); 232 233 $.each(gval.rules, function(i, rule){ 234 if(!gval.check_rule(rule)){ 235 gval.invalid_rule = rule; 236 toReturn = false; 237 return false;//break 238 } 239 }); 240 241 if(toReturn == false){ 242 $(gval.element).trigger('invalid.gvalidation'); 243 } 244 245 if(gval.disabled == true){ 246 return true; 247 } 248 if($(gval.element).is(':hidden')){ 249 return true; 250 } 251 252 return toReturn; 253 }, 254 255 set_rules: function(rules){ 256 var gval = this; 257 gval.rules = rules; 258 }, 259 260 check_rule: function(rule){ 261 var gval = this; 262 if($(gval.element).prop('disabled')){ 263 return true; 264 } 265 var $type = $(gval.element).prop('type'); 266 var $rule_parts = rule.split(':'); 267 if($.gvalidation.rules.hasOwnProperty($rule_parts[0])){ 268 if($.inArray($type, ['checkbox','radio']) > -1){ 269 return $(gval.element).prop('checked'); 270 }else{ 271 if($rule_parts[0] == 'required'){ 272 if($(gval.element).val() == null){ 273 return false;//for multi select with nothing selected 274 }else if($.isArray($(gval.element).val())){ 275 if($(gval.element).val().length == 0){ 276 return false;//for multi select 277 }else{ 278 return true; 279 } 280 }else{ 281 return $(gval.element).val().trim().match($.gvalidation.rules[$rule_parts[0]]); 282 } 283 }else{ 284 return (!$(gval.element).val().trim() || $(gval.element).val().trim().match($.gvalidation.rules[$rule_parts[0]])); 285 } 286 } 287 }else{ 288 if($rule_parts[0] == 'group'){ 289 var count = 0; 290 $('input[class*="'+rule+'"]').each(function(i, chk){ 291 if($(chk).prop('checked')){ 292 count = count + 1; 293 } 294 }); 295 var limit = ($rule_parts[2] ? $rule_parts[2] : 1); 296 return (count >= limit); 297 }else if($rule_parts[0] == 'confirm'){ 298 return ($('#'+$rule_parts[1]).val() == $(gval.element).val()); 299 }else if($rule_parts[0] == 'custom'){ 300 var fn = $rule_parts[1]; 301 if(fn in window){ 302 return window[fn]($(gval.element)); 303 } 304 return true; 305 } 306 } 307 }, 308 309 show_error: function(){ 310 var gval = this; 311 312 if(!gval.invalid_rule){ 313 return; 314 } 315 316 var $rule_parts = gval.invalid_rule.split(':'); 317 rule = $rule_parts[0]; 318 if(typeof gval.errors[rule] == 'undefined'){ 319 if($(gval.element).prop('title')){ 320 gval.errors[rule] = $(gval.element).prop('title'); 321 }else{ 322 var error_string = $.gvalidation.errors[rule]; 323 if(rule == "group"){ 324 var skip = false; 325 $('input[class*="'+gval.invalid_rule+'"]').each(function(i, chk){ 326 var gval_other = $(chk).gvalidate('get'); 327 var another_error_shown = (gval_other == null ? false : gval_other.error_shown); 328 if(another_error_shown !== false){ 329 skip = true; 330 return false; 331 } 332 });
333 if(skip){ 334 return; 335 } 336 } 337 if(rule == "group"){ 338 if(typeof $rule_parts[2] == 'undefined'){ 339 //dirty fix for the group validtaions 340 $rule_parts[1] = 1; 341 } 342 } 343 $.each($rule_parts, function(i, val){ 344 error_string = error_string.replace('%'+i, val); 345 }); 346 gval.errors[rule] = error_string; 347 } 348 } 349 350 gval.display_error(); 351 gval.error_shown = true; 352 }, 353 354 display_error: function(){ 355 var gval = this; 356 357 $(gval.element).trigger('display.gvalidation'); 358 359 var error_target = $(gval.element); 360 if($(gval.element).data('gvalidation-target')){ 361 var error_target = $(gval.element).data('gvalidation-target'); 362 } 363 364 if($.gvalidation.display == 'tooltip'){ 365 error_target.data('content', '<span class="gvalidation-error-text">'+gval.errors[gval.rule_name(gval.invalid_rule)]+'</span>'); 366 error_target.gtooltip({'tipclass':'gtooltip gvalidation-error-tip', 'closable': 1, 'tid':'gval', 'trigger':'manual', 'css':gval.settings.css}); 367 error_target.gtooltip('reset', {'tid':'gval'}); 368 error_target.gtooltip('show', {'tid':'gval'}); 369 }else{ 370 //$(gval.element).parent().after($($(gval.element).data('content')).css('display', 'block')); 371 } 372 }, 373 374 remove_error: function(){ 375 var gval = this; 376 377 if(!gval.invalid_rule){ 378 return; 379 } 380 381 gval.errors = {}; 382 gval.error_shown = false; 383 gval.invalid_rule = ''; 384 385 if($.gvalidation.display == 'tooltip'){ 386 $(gval.element).gtooltip('destroy', {'tid':'gval'}); 387 }else{ 388 389 } 390 }, 391 392 disable: function(){ 393 var gval = this; 394 395 gval.disabled = true; 396 }, 397 398 enable: function(){ 399 var gval = this; 400 401 gval.disabled = false; 402 }, 403 404 rule_name: function(rule){ 405 var $rule_parts = rule.split(':'); 406 return $rule_parts[0]; 407 }, 408 409 rule_params: function(rule){ 410 var $rule_parts = rule.split(':'); 411 $rule_parts.splice(0, 1); 412 return $rule_parts; 413 }, 414 415 }; 416}(jQuery));
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.