1/** 2 * jQuery Validation Plugin 1.9.0 3 * 4 * http://bassistance.de/jquery-plugins/jquery-plugin-validation/ 5 * http://docs.jquery.com/Plugins/Validation 6 * 7 * Copyright (c) 2006 - 2011 Jörn Zaefferer 8 * 9 * Dual licensed under the MIT and GPL licenses: 10 * http://www.opensource.org/licenses/mit-license.php 11 * http://www.gnu.org/licenses/gpl.html 12 */ 13 14(function($) { 15 16$.extend($.fn, { 17 // http://docs.jquery.com/Plugins/Validation/validate 18 validate: function( options ) { 19 20 // if nothing is selected, return nothing; can't chain anyway 21 if (!this.length) { 22 options && options.debug && window.console && console.warn( "nothing selected, can't validate, returning nothing" ); 23 return; 24 } 25 26 // check if a validator for this form was already created 27 var validator = $.data(this[0], 'validator'); 28 if ( validator ) { 29 return validator; 30 } 31 32 // Add novalidate tag if HTML5. 33 this.attr('novalidate', 'novalidate'); 34 35 validator = new $.validator( options, this[0] ); 36 $.data(this[0], 'validator', validator); 37 38 if ( validator.settings.onsubmit ) { 39 40 var inputsAndButtons = this.find("input, button"); 41 42 // allow suppresing validation by adding a cancel class to the submit button 43 inputsAndButtons.filter(".cancel").click(function () { 44 validator.cancelSubmit = true; 45 }); 46 47 // when a submitHandler is used, capture the submitting button 48 if (validator.settings.submitHandler) { 49 inputsAndButtons.filter(":submit").click(function () { 50 validator.submitButton = this; 51 }); 52 } 53 54 // validate the form on submit 55 this.submit( function( event ) { 56 if ( validator.settings.debug ) 57 // prevent form submit to be able to see console output 58 event.preventDefault(); 59 60 function handle() { 61 if ( validator.settings.submitHandler ) { 62 if (validator.submitButton) { 63 // insert a hidden input as a replacement for the missing submit button 64 var hidden = $("<input type='hidden'/>").attr("name", validator.submitButton.name).val(validator.submitButton.value).appendTo(validator.currentForm); 65 } 66 validator.settings.submitHandler.call( validator, validator.currentForm ); 67 if (validator.submitButton) { 68 // and clean up afterwards; thanks to no-block-scope, hidden can be referenced 69 hidden.remove(); 70 } 71 return false; 72 } 73 return true; 74 } 75 76 // prevent submit for invalid forms or custom submit handlers 77 if ( validator.cancelSubmit ) { 78 validator.cancelSubmit = false; 79 return handle(); 80 } 81 if ( validator.form() ) { 82 if ( validator.pendingRequest ) { 83 validator.formSubmitted = true; 84 return false; 85 } 86 return handle(); 87 } else { 88 validator.focusInvalid(); 89 return false; 90 } 91 }); 92 } 93 94 return validator; 95 }, 96 // http://docs.jquery.com/Plugins/Validation/valid 97 valid: function() { 98 if ( $(this[0]).is('form')) { 99 return this.validate().form(); 100 } else { 101 var valid = true; 102 var validator = $(this[0].form).validate(); 103 this.each(function() { 104 valid &= validator.element(this); 105 }); 106 return valid; 107 } 108 }, 109 // attributes: space seperated list of attributes to retrieve and remove 110 removeAttrs: function(attributes) { 111 var result = {}, 112 $element = this; 113 $.each(attributes.split(/\s/), function(index, value) { 114 result[value] = $element.attr(value); 115 $element.removeAttr(value); 116 }); 117 return result; 118 }, 119 // http://docs.jquery.com/Plugins/Validation/rules 120 rules: function(command, argument) { 121 var element = this[0]; 122 123 if (command) { 124 var settings = $.data(element.form, 'validator').settings; 125 var staticRules = settings.rules; 126 var existingRules = $.validator.staticRules(element); 127 switch(command) { 128 case "add": 129 $.extend(existingRules, $.validator.normalizeRule(argument)); 130 staticRules[element.name] = existingRules; 131 if (argument.messages) 132 settings.messages[element.name] = $.extend( settings.messages[element.name], argument.messages ); 133 break; 134 case "remove": 135 if (!argument) { 136 delete staticRules[element.name]; 137 return existingRules; 138 } 139 var filtered = {}; 140 $.each(argument.split(/\s/), function(index, method) { 141 filtered[method] = existingRules[method]; 142 delete existingRules[method]; 143 }); 144 return filtered; 145 } 146 } 147 148 var data = $.validator.normalizeRules( 149 $.extend( 150 {}, 151 $.validator.metadataRules(element), 152 $.validator.classRules(element), 153 $.validator.attributeRules(element), 154 $.validator.staticRules(element) 155 ), element); 156 157 // make sure required is at front 158 if (data.required) { 159 var param = data.required; 160 delete data.required; 161 data = $.extend({required: param}, data); 162 } 163 164 return data; 165 } 166}); 167 168// Custom selectors 169$.extend($.expr[":"], { 170 // http://docs.jquery.com/Plugins/Validation/blank 171 blank: function(a) {return !$.trim("" + a.value);}, 172 // http://docs.jquery.com/Plugins/Validation/filled 173 filled: function(a) {return !!$.trim("" + a.value);}, 174 // http://docs.jquery.com/Plugins/Validation/unchecked 175 unchecked: function(a) {return !a.checked;} 176}); 177 178// constructor for validator 179$.validator = function( options, form ) { 180 this.settings = $.extend( true, {}, $.validator.defaults, options ); 181 this.currentForm = form; 182 this.init(); 183}; 184 185$.validator.format = function(source, params) { 186 if ( arguments.length == 1 ) 187 return function() { 188 var args = $.makeArray(arguments); 189 args.unshift(source); 190 return $.validator.format.apply( this, args ); 191 }; 192 if ( arguments.length > 2 && params.constructor != Array ) { 193 params = $.makeArray(arguments).slice(1); 194 } 195 if ( params.constructor != Array ) { 196 params = [ params ]; 197 } 198 $.each(params, function(i, n) { 199 source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n); 200 }); 201 return source; 202}; 203 204$.extend($.validator, { 205 206 defaults: { 207 messages: {}, 208 groups: {}, 209 rules: {}, 210 errorClass: "error", 211 validClass: "valid", 212 errorElement: "div", 213 focusInvalid: true, 214 errorContainer: $( [] ), 215 errorLabelContainer: $( [] ), 216 onsubmit: true, 217 ignore: ":hidden", 218 ignoreTitle: false, 219 onfocusin: function(element, event) { 220 this.lastActive = element; 221 222 // hide error label and remove error class on focus if enabled 223 if ( this.settings.focusCleanup && !this.blockFocusCleanup ) { 224 this.settings.unhighlight && this.settings.unhighlight.call( this, element, this.settings.errorClass, this.settings.val
224idClass ); 225 this.addWrapper(this.errorsFor(element)).hide(); 226 } 227 }, 228 onfocusout: function(element, event) { 229 if ( !this.checkable(element) && (element.name in this.submitted || !this.optional(element)) ) { 230 this.element(element); 231 } 232 }, 233 onkeyup: function(element, event) { 234 if ( element.name in this.submitted || element == this.lastElement ) { 235 this.element(element); 236 } 237 }, 238 onclick: function(element, event) { 239 // click on selects, radiobuttons and checkboxes 240 if ( element.name in this.submitted ) 241 this.element(element); 242 // or option elements, check parent select in that case 243 else if (element.parentNode.name in this.submitted) 244 this.element(element.parentNode); 245 }, 246 highlight: function(element, errorClass, validClass) { 247 if (element.type === 'radio') { 248 this.findByName(element.name).addClass(errorClass).removeClass(validClass); 249 } else { 250 $(element).addClass(errorClass).removeClass(validClass); 251 } 252 }, 253 unhighlight: function(element, errorClass, validClass) { 254 if (element.type === 'radio') { 255 this.findByName(element.name).removeClass(errorClass).addClass(validClass); 256 } else { 257 $(element).removeClass(errorClass).addClass(validClass); 258 } 259 } 260 }, 261 262 // http://docs.jquery.com/Plugins/Validation/Validator/setDefaults 263 setDefaults: function(settings) { 264 $.extend( $.validator.defaults, settings ); 265 }, 266 267 messages: { 268 required: "This field is required.", 269 remote: "Please fix this field.", 270 email: "Please enter a valid email address.", 271 url: "Please enter a valid URL.", 272 date: "Please enter a valid date.", 273 dateISO: "Please enter a valid date (ISO).", 274 number: "Please enter a valid number.", 275 digits: "Please enter only digits.", 276 creditcard: "Please enter a valid credit card number.", 277 equalTo: "Please enter the same value again.", 278 accept: "Please enter a valid extension.", 279 acceptJS: "Please enter a value with a valid font extension in JS.", 280 maxlength: $.validator.format("Please enter no more than {0} characters."), 281 minlength: $.validator.format("Please enter at least {0} characters."), 282 rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."), 283 range: $.validator.format("Please enter a value between {0} and {1}."), 284 max: $.validator.format("Please enter a value less than or equal to {0}."), 285 min: $.validator.format("Please enter a value greater than or equal to {0}.") 286 }, 287 288 autoCreateRanges: false, 289 290 prototype: { 291 292 init: function() { 293 this.labelContainer = $(this.settings.errorLabelContainer); 294 this.errorContext = this.labelContainer.length && this.labelContainer || $(this.currentForm); 295 this.containers = $(this.settings.errorContainer).add( this.settings.errorLabelContainer ); 296 this.submitted = {}; 297 this.valueCache = {}; 298 this.pendingRequest = 0; 299 this.pending = {}; 300 this.invalid = {}; 301 this.reset(); 302 303 var groups = (this.groups = {}); 304 $.each(this.settings.groups, function(key, value) { 305 $.each(value.split(/\s/), function(index, name) { 306 groups[name] = key; 307 }); 308 }); 309 var rules = this.settings.rules; 310 $.each(rules, function(key, value) { 311 rules[key] = $.validator.normalizeRule(value); 312 }); 313 314 function delegate(event) { 315 var validator = $.data(this[0].form, "validator"), 316 eventType = "on" + event.type.replace(/^validate/, ""); 317 validator.settings[eventType] && validator.settings[eventType].call(validator, this[0], event); 318 } 319 $(this.currentForm) 320 .validateDelegate("[type='text'], [type='password'], [type='file'], select, textarea, " + 321 "[type='number'], [type='search'] ,[type='tel'], [type='url'], " + 322 "[type='email'], [type='datetime'], [type='date'], [type='month'], " + 323 "[type='week'], [type='time'], [type='datetime-local'], " + 324 "[type='range'], [type='color'] ", 325 "focusin focusout keyup", delegate) 326 .validateDelegate("[type='radio'], [type='checkbox'], select, option", "click", delegate); 327 328 if (this.settings.invalidHandler) 329 $(this.currentForm).bind("invalid-form.validate", this.settings.invalidHandler); 330 }, 331 332 // http://docs.jquery.com/Plugins/Validation/Validator/form 333 form: function() { 334 this.checkForm(); 335 $.extend(this.submitted, this.errorMap); 336 this.invalid = $.extend({}, this.errorMap); 337 if (!this.valid()) 338 $(this.currentForm).triggerHandler("invalid-form", [this]); 339 this.showErrors(); 340 return this.valid(); 341 }, 342 343 checkForm: function() { 344 this.prepareForm(); 345 for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) { 346 this.check( elements[i] ); 347 } 348 return this.valid(); 349 }, 350 351 // http://docs.jquery.com/Plugins/Validation/Validator/element 352 element: function( element ) { 353 element = this.validationTargetFor( this.clean( element ) ); 354 this.lastElement = element; 355 this.prepareElement( element ); 356 this.currentElements = $(element); 357 var result = this.check( element ); 358 if ( result ) { 359 delete this.invalid[element.name]; 360 } else { 361 this.invalid[element.name] = true; 362 } 363 if ( !this.numberOfInvalids() ) { 364 // Hide error containers on last error 365 this.toHide = this.toHide.add( this.containers ); 366 } 367 this.showErrors(); 368 return result; 369 }, 370 371 // http://docs.jquery.com/Plugins/Validation/Validator/showErrors 372 showErrors: function(errors) { 373 if(errors) { 374 // add items to error list and map 375 $.extend( this.errorMap, errors ); 376 this.errorList = []; 377 for ( var name in errors ) { 378 this.errorList.push({ 379 message: errors[name], 380 element: this.findByName(name)[0] 381 });
382 } 383 // remove items from success list 384 this.successList = $.grep( this.successList, function(element) { 385 return !(element.name in errors); 386 }); 387 } 388 this.settings.showErrors 389 ? this.settings.showErrors.call( this, this.errorMap, this.errorList ) 390 : this.defaultShowErrors(); 391 }, 392 393 // http://docs.jquery.com/Plugins/Validation/Validator/resetForm 394 resetForm: function() { 395 if ( $.fn.resetForm ) 396 $( this.currentForm ).resetForm(); 397 this.submitted = {}; 398 this.lastElement = null; 399 this.prepareForm(); 400 this.hideErrors(); 401 this.elements().removeClass( this.settings.errorClass ); 402 }, 403 404 numberOfInvalids: function() { 405 return this.objectLength(this.invalid); 406 }, 407 408 objectLength: function( obj ) { 409 var count = 0; 410 for ( var i in obj ) 411 count++; 412 return count; 413 }, 414 415 hideErrors: function() { 416 this.addWrapper( this.toHide ).hide(); 417 }, 418 419 valid: function() { 420 return this.size() == 0; 421 }, 422 423 size: function() { 424 return this.errorList.length; 425 }, 426 427 focusInvalid: function() { 428 if( this.settings.focusInvalid ) { 429 try { 430 $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []) 431 .filter(":visible") 432 .focus() 433 // manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find 434 .trigger("focusin"); 435 } catch(e) { 436 // ignore IE throwing errors when focusing hidden elements 437 } 438 } 439 }, 440 441 findLastActive: function() { 442 var lastActive = this.lastActive; 443 return lastActive && $.grep(this.errorList, function(n) { 444 return n.element.name == lastActive.name; 445 }).length == 1 && lastActive; 446 }, 447 448 elements: function() { 449 var validator = this, 450 rulesCache = {}; 451 452 // select all valid inputs inside the form (no submit or reset buttons) 453 return $(this.currentForm) 454 .find("input, select, textarea") 455 .not(":submit, :reset, :image, [disabled]") 456 .not( this.settings.ignore ) 457 .filter(function() { 458 !this.name && validator.settings.debug && window.console && console.error( "%o has no name assigned", this); 459 460 // select only the first element for each name, and only those with rules specified 461 if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) 462 return false; 463 464 rulesCache[this.name] = true; 465 return true; 466 }); 467 }, 468 469 clean: function( selector ) { 470 return $( selector )[0]; 471 }, 472 473 errors: function() { 474 return $( this.settings.errorElement + "." + this.settings.errorClass, this.errorContext ); 475 }, 476 477 reset: function() { 478 this.successList = []; 479 this.errorList = []; 480 this.errorMap = {}; 481 this.toShow = $([]); 482 this.toHide = $([]); 483 this.currentElements = $([]); 484 }, 485 486 prepareForm: function() { 487 this.reset(); 488 this.toHide = this.errors().add( this.containers ); 489 }, 490 491 prepareElement: function( element ) { 492 this.reset(); 493 this.toHide = this.errorsFor(element); 494 }, 495 496 check: function( element ) { 497 element = this.validationTargetFor( this.clean( element ) ); 498 499 var rules = $(element).rules(); 500 var dependencyMismatch = false; 501 for (var method in rules ) { 502 var rule = { method: method, parameters: rules[method] }; 503 try { 504 var result = $.validator.methods[method].call( this, element.value.replace(/\r/g, ""), element, rule.parameters ); 505 506 // if a method indicates that the field is optional and therefore valid, 507 // don't mark it as valid when there are no other rules 508 if ( result == "dependency-mismatch" ) { 509 dependencyMismatch = true; 510 continue; 511 } 512 dependencyMismatch = false; 513 514 if ( result == "pending" ) { 515 this.toHide = this.toHide.not( this.errorsFor(element) ); 516 return; 517 } 518 519 if( !result ) { 520 this.formatAndAdd( element, rule ); 521 return false; 522 } 523 } catch(e) { 524 this.settings.debug && window.console && console.log("exception occured when checking element " + element.id
525 + ", check the '" + rule.method + "' method", e); 526 throw e; 527 } 528 } 529 if (dependencyMismatch) 530 return; 531 if ( this.objectLength(rules) ) 532 this.successList.push(element); 533 return true; 534 }, 535 536 // return the custom message for the given element and validation method 537 // specified in the element's "messages" metadata 538 customMetaMessage: function(element, method) { 539 if (!$.metadata) 540 return; 541 542 var meta = this.settings.meta 543 ? $(element).metadata()[this.settings.meta] 544 : $(element).metadata(); 545 546 return meta && meta.messages && meta.messages[method]; 547 }, 548 549 // return the custom message for the given element name and validation method 550 customMessage: function( name, method ) { 551 var m = this.settings.messages[name]; 552 return m && (m.constructor == String 553 ? m 554 : m[method]); 555 }, 556 557 // return the first defined argument, allowing empty strings 558 findDefined: function() { 559 for(var i = 0; i < arguments.length; i++) { 560 if (arguments[i] !== undefined) 561 return arguments[i]; 562 } 563 return undefined; 564 }, 565 566 defaultMessage: function( element, method) { 567 return this.findDefined( 568 this.customMessage( element.name, method ), 569 this.customMetaMessage( element, method ), 570 // title is never undefined, so handle empty string as undefined 571 !this.settings.ignoreTitle && element.title || undefined, 572 $.validator.messages[method], 573 "<strong>Warning: No message defined for " + element.name + "</strong>" 574 ); 575 }, 576 577 formatAndAdd: function( element, rule ) { 578 var message = this.defaultMessage( element, rule.method ), 579 theregex = /\$?\{(\d+)\}/g; 580 if ( typeof message == "function" ) { 581 message = message.call(this, rule.parameters, element); 582 } else if (theregex.test(message)) { 583 message = jQuery.format(message.replace(theregex, '{$1}'), rule.parameters); 584 } 585 this.errorList.push({ 586 message: message, 587 element: element 588 }); 589 590 this.errorMap[element.name] = message; 591 this.submitted[element.name] = message; 592 }, 593 594 addWrapper: function(toToggle) { 595 if ( this.settings.wrapper ) 596 toToggle = toToggle.add( toToggle.parent( this.settings.wrapper ) ); 597 return toToggle; 598 }, 599 600 defaultShowErrors: function() { 601 for ( var i = 0; this.errorList[i]; i++ ) { 602 var error = this.errorList[i]; 603 this.settings.highlight && this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass ); 604 this.showLabel( error.element, error.message ); 605 } 606 if( this.errorList.length ) { 607 this.toShow = this.toShow.add( this.containers ); 608 } 609 if (this.settings.success) { 610 for ( var i = 0; this.successList[i]; i++ ) { 611 this.showLabel( this.successList[i] ); 612 } 613 } 614 if (this.settings.unhighlight) { 615 for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) { 616 this.settings.unhighlight.call( this, elements[i], this.settings.errorClass, this.settings.validClass ); 617 } 618 } 619 this.toHide = this.toHide.not( this.toShow ); 620 this.hideErrors(); 621 this.addWrapper( this.toShow ).show(); 622 }, 623 624 validElements: function() { 625 return this.currentElements.not(this.invalidElements()); 626 }, 627 628 invalidElements: function() { 629 return $(this.errorList).map(function() { 630 return this.element; 631 }); 632 }, 633 634 showLabel: function(element, message) { 635 var label = this.errorsFor( element ); 636 if ( label.length ) { 637 // refresh error/success class 638 label.removeClass( this.settings.validClass ).addClass( this.settings.errorClass ); 639 640 // check if we have a generated label, replace the message then 641 label.attr("generated") && label.html(message); 642 } else { 643 // create label 644 label = $("<" + this.settings.errorElement + "/>") 645 .attr({"for": this.idOrName(element), generated: true}) 646 .addClass(this.settings.errorClass) 647 .html(message || ""); 648 if ( this.settings.wrapper ) { 649 // make sure the element is visible, even in IE 650 // actually showing the wrapped element is handled elsewhere 651 label = label.hide().show().wrap("<" + this.settings.wrapper + "/>").parent(); 652 } 653 if ( !this.labelContainer.append(label).length ) 654 this.settings.errorPlacement 655 ? this.settings.errorPlacement(label, $(element) ) 656 : label.insertAfter(element); 657 } 658 if ( !message && this.settings.success ) { 659 label.text(""); 660 typeof this.settings.success == "string" 661 ? label.addClass( this.settings.success ) 662 : this.settings.success( label ); 663 } 664 this.toShow = this.toShow.add(label); 665 }, 666 667 errorsFor: function(element) { 668 var name = this.idOrName(element); 669 return this.errors().filter(function() { 670 return $(this).attr('for') == name; 671 }); 672 }, 673 674 idOrName: function(element) { 675 return this.groups[element.name] || (this.checkable(element) ? element.name : element.i
675d || element.name); 676 }, 677 678 validationTargetFor: function(element) { 679 // if radio/checkbox, validate first element in group instead 680 if (this.checkable(element)) { 681 element = this.findByName( element.name ).not(this.settings.ignore)[0]; 682 } 683 return element; 684 }, 685 686 checkable: function( element ) { 687 return /radio|checkbox/i.test(element.type); 688 }, 689 690 findByName: function( name ) { 691 // select by name and filter by form for performance over form.find("[name=...]") 692 var form = this.currentForm; 693 return $(document.getElementsByName(name)).map(function(index, element) { 694 return element.form == form && element.name == name && element || null; 695 }); 696 }, 697 698 getLength: function(value, element) { 699 switch( element.nodeName.toLowerCase() ) { 700 case 'select': 701 return $("option:selected", element).length; 702 case 'input': 703 if( this.checkable( element) ) 704 return this.findByName(element.name).filter(':checked').length; 705 } 706 return value.length; 707 }, 708 709 depen
709d: function(param, element) { 710 return this.dependTypes[typeof param] 711 ? this.dependTypes[typeof param](param, element) 712 : true; 713 }, 714 715 dependTypes: { 716 "boolean": function(param, element) { 717 return param; 718 }, 719 "string": function(param, element) { 720 return !!$(param, element.form).length; 721 }, 722 "function": function(param, element) { 723 return param(element); 724 } 725 }, 726 727 optional: function(element) { 728 return !$.validator.methods.required.call(this, $.trim(element.value), element) && "dependency-mismatch"; 729 }, 730 731 startRequest: function(element) { 732 if (!this.pending[element.name]) { 733 this.pendingRequest++; 734 this.pending[element.name] = true; 735 } 736 }, 737 738 stopRequest: function(element, valid) { 739 this.pendingRequest--; 740 // sometimes synchronization fails, make sure pendingRequest is never < 0 741 if (this.pendingRequest < 0) 742 this.pendingRequest = 0; 743 delete this.pending[element.name]; 744 if ( valid && this.pendingRequest == 0 && this.formSubmitted && this.form() ) { 745 $(this.currentForm).submit(); 746 this.formSubmitted = false; 747 } else if (!valid && this.pendingRequest == 0 && this.formSubmitted) { 748 $(this.currentForm).triggerHandler("invalid-form", [this]); 749 this.formSubmitted = false; 750 } 751 }, 752 753 previousValue: function(element) { 754 return $.data(element, "previousValue") || $.data(element, "previousValue", { 755 old: null, 756 valid: true, 757 message: this.defaultMessage( element, "remote" ) 758 }); 759 } 760 761 }, 762 763 classRuleSettings: { 764 required: {required: true}, 765 email: {email: true}, 766 url: {url: true}, 767 date: {date: true}, 768 dateISO: {dateISO: true}, 769 dateDE: {dateDE: true}, 770 number: {number: true}, 771 numberDE: {numberDE: true}, 772 digits: {digits: true}, 773 creditcard: {creditcard: true} 774 }, 775 776 addClassRules: function(className, rules) { 777 className.constructor == String ? 778 this.classRuleSettings[className] = rules : 779 $.extend(this.classRuleSettings, className); 780 }, 781 782 classRules: function(element) { 783 var rules = {}; 784 var classes = $(element).attr('class'); 785 classes && $.each(classes.split(' '), function() { 786 if (this in $.validator.classRuleSettings) { 787 $.extend(rules, $.validator.classRuleSettings[this]); 788 } 789 }); 790 return rules; 791 }, 792 793 attributeRules: function(element) { 794 var rules = {}; 795 var $element = $(element); 796 797 for (var method in $.validator.methods) { 798 var value; 799 // If .prop exists (jQuery >= 1.6), use it to get true/false for required 800 if (method === 'required' && typeof $.fn.prop === 'function') { 801 value = $element.prop(method); 802 } else { 803 value = $element.attr(method); 804 } 805 if (value) { 806 rules[method] = value; 807 } else if ($element[0].getAttribute("type") === method) { 808 rules[method] = true; 809 } 810 } 811 812 // maxlength may be returned as -1, 2147483647 (IE) and 524288 (safari) for text inputs 813 if (rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength)) { 814 delete rules.maxlength; 815 } 816 817 return rules; 818 }, 819 820 metadataRules: function(element) { 821 if (!$.metadata) return {}; 822 823 var meta = $.data(element.form, 'validator').settings.meta; 824 return meta ? 825 $(element).metadata()[meta] : 826 $(element).metadata(); 827 }, 828 829 staticRules: function(element) { 830 var rules = {}; 831 var validator = $.data(element.form, 'validator'); 832 if (validator.settings.rules) { 833 rules = $.validator.normalizeRule(validator.settings.rules[element.name]) || {}; 834 } 835 return rules; 836 }, 837 838 normalizeRules: function(rules, element) { 839 // handle dependency check 840 $.each(rules, function(prop, val) { 841 // ignore rule when param is explicitly false, eg. required:false 842 if (val === false) { 843 delete rules[prop]; 844 return; 845 } 846 if (val.param || val.depends) { 847 var keepRule = true; 848 switch (typeof val.depends) { 849 case "string": 850 keepRule = !!$(val.depends, element.form).length; 851 break; 852 case "function": 853 keepRule = val.depends.call(element, element); 854 break; 855 } 856 if (keepRule) { 857 rules[prop] = val.param !== undefined ? val.param : true; 858 } else { 859 delete rules[prop]; 860 } 861 } 862 }); 863 864 // evaluate parameters 865 $.each(rules, function(rule, parameter) { 866 rules[rule] = $.isFunction(parameter) ? parameter(element) : parameter; 867 }); 868 869 // clean number parameters 870 $.each(['minlength', 'maxlength', 'min', 'max'], function() { 871 if (rules[this]) { 872 rules[this] = Number(rules[this]); 873 } 874 }); 875 $.each(['rangelength', 'range'], function() { 876 if (rules[this]) { 877 rules[this] = [Number(rules[this][0]), Number(rules[this][1])]; 878 } 879 }); 880 881 if ($.validator.autoCreateRanges) { 882 // auto-create ranges 883 if (rules.min && rules.max) { 884 rules.range = [rules.min, rules.max]; 885 delete rules.min; 886 delete rules.max; 887 } 888 if (rules.minlength && rules.maxlength) { 889 rules.rangelength = [rules.minlength, rules.maxlength]; 890 delete rules.minlength; 891 delete rules.maxlength; 892 } 893 } 894 895 // To support custom messages in metadata ignore rule methods titled "messages" 896 if (rules.messages) { 897 delete rules.messages; 898 } 899 900 return rules; 901 }, 902
903 // Converts a simple string to a {string: true} rule, e.g., "required" to {required:true} 904 normalizeRule: function(data) { 905 if( typeof data == "string" ) { 906 var transformed = {}; 907 $.each(data.split(/\s/), function() { 908 transformed[this] = true; 909 }); 910 data = transformed; 911 } 912 return data; 913 }, 914 915 // http://docs.jquery.com/Plugins/Validation/Validator/addMethod 916 addMethod: function(name, method, message) { 917 $.validator.methods[name] = method; 918 $.validator.messages[name] = message != undefined ? message : $.validator.messages[name]; 919 if (method.length < 3) { 920 $.validator.addClassRules(name, $.validator.normalizeRule(name)); 921 } 922 }, 923 924 methods: { 925 926 // http://docs.jquery.com/Plugins/Validation/Methods/required 927 required: function(value, element, param) { 928 // check if dependency is met 929 if ( !this.depend(param, element) ) 930 return "dependency-mismatch"; 931 switch( element.nodeName.toLowerCase() ) { 932 case 'select': 933 // could be an array for select-multiple or a string, both are fine this way 934 var val = $(element).val(); 935 return val && val.length > 0; 936 case 'input': 937 if ( this.checkable(element) ) 938 return this.getLength(value, element) > 0; 939 default: 940 return $.trim(value).length > 0; 941 } 942 }, 943 944 // http://docs.jquery.com/Plugins/Validation/Methods/remote 945 remote: function(value, element, param) { 946 if ( this.optional(element) ) 947 return "dependency-mismatch"; 948 949 var previous = this.previousValue(element); 950 if (!this.settings.messages[element.name] ) 951 this.settings.messages[element.name] = {}; 952 previous.originalMessage = this.settings.messages[element.name].remote; 953 this.settings.messages[element.name].remote = previous.message; 954 955 param = typeof param == "string" && {url:param} || param; 956 957 if ( this.pending[element.name] ) { 958 return "pending"; 959 } 960 if ( previous.old === value ) { 961 return previous.valid; 962 } 963 964 previous.old = value; 965 var validator = this; 966 this.startRequest(element); 967 var data = {}; 968 data[element.name] = value; 969 $.ajax($.extend(true, { 970 url: param, 971 mode: "abort", 972 port: "validate" + element.name, 973 dataType: "json", 974 data: data, 975 success: function(response) { 976 validator.settings.messages[element.name].remote = previous.originalMessage; 977 var valid = response === true; 978 if ( valid ) { 979 var submitted = validator.formSubmitted; 980 validator.prepareElement(element); 981 validator.formSubmitted = submitted; 982 validator.successList.push(element); 983 validator.showErrors(); 984 } else { 985 var errors = {}; 986 var message = response || validator.defaultMessage( element, "remote" ); 987 errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message; 988 validator.showErrors(errors); 989 } 990 previous.valid = valid; 991 validator.stopRequest(element, valid); 992 } 993 }, param)); 994 return "pending"; 995 }, 996 997 // http://docs.jquery.com/Plugins/Validation/Methods/minlength 998 minlength: function(value, element, param) { 999 return this.optional(element) || this.getLength($.trim(value), element) >= param; 1000 }, 1001 1002 // http://docs.jquery.com/Plugins/Validation/Methods/maxlength 1003 maxlength: function(value, element, param) { 1004 return this.optional(element) || this.getLength($.trim(value), element) <= param; 1005 }, 1006 1007 // http://docs.jquery.com/Plugins/Validation/Methods/rangelength 1008 rangelength: function(value, element, param) { 1009 var length = this.getLength($.trim(value), element); 1010 return this.optional(element) || ( length >= param[0] && length <= param[1] ); 1011 }, 1012 1013 // http://docs.jquery.com/Plugins/Validation/Methods/min 1014 min: function( value, element, param ) { 1015 return this.optional(element) || value >= param; 1016 }, 1017 1018 // http://docs.jquery.com/Plugins/Validation/Methods/max 1019 max: function( value, element, param ) { 1020 return this.optional(element) || value <= param; 1021 }, 1022 1023 // http://docs.jquery.com/Plugins/Validation/Methods/range 1024 range: function( value, element, param ) { 1025 return this.optional(element) || ( value >= param[0] && value <= param[1] ); 1026 }, 1027 1028 // http://docs.jquery.com/Plugins/Validation/Methods/email 1029 email: function(value, element) { 1030 // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/ 1031 return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value); 1032 }, 1033 1034 // http://docs.jquery.com/Plugins/Validation/Methods/url 1035 url: function(value, element) { 1036 // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/ 1037 return this.optional(element) || /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value); 1038 }, 1039 1040 // http://docs.jquery.com/Plugins/Validation/Methods/date 1041 date: function(value, element) { 1042 return this.optional(element) || !/Invalid|NaN/.test(new Date(value)); 1043 }, 1044 1045 // http://docs.jquery.com/Plugins/Validation/Methods/dateISO 1046 dateISO: function(value, element) { 1047 return this.optional(element) || /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(value); 1048 }, 1049 1050 // http://docs.jquery.com/Plugins/Validation/Methods/number 1051 number: function(value, element) { 1052 return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value); 1053 }, 1054 1055 // http://docs.jquery.com/Plugins/Validation/Methods/digits 1056 digits: function(value, element) { 1057 return this.optional(element) || /^\d+$/.test(value); 1058 }, 1059 1060 // http://docs.jquery.com/Plugins/Validation/Methods/creditcard 1061 // based on http://en.wikipedia.org/wiki/Luhn 1062 creditcard: function(value, element) { 1063 if ( this.optional(element) ) 1064 return "dependency-mismatch"; 1065 // accept only spaces, digits and dashes 1066 if (/[^0-9 -]+/.test(value)) 1067 return false; 1068 var nCheck = 0, 1069 nDigit = 0, 1070 bEven = false; 1071 1072 value = value.replace(/\D/g, ""); 1073 1074 for (var n = value.length - 1; n >= 0; n--) { 1075 var cDigit = value.charAt(n); 1076 var nDigit = parseInt(cDigit, 10); 1077 if (bEven) { 1078 if ((nDigit *= 2) > 9) 1079 nDigit -= 9; 1080 } 1081 nCheck += nDigit; 1082 bEven = !bEven; 1083 } 1084 1085 return (nCheck % 10) == 0;
1086 }, 1087 1088 // http://docs.jquery.com/Plugins/Validation/Methods/accept 1089 accept: function(value, element, param) { 1090 param = typeof param == "string" ? param.replace(/,/g, '|') : "png|jpe?g|gif|bmp"; 1091 return this.optional(element) || value.match(new RegExp(".(" + param + ")$", "i")); 1092 }, 1093 1094 acceptJS: function(value, element, param) { 1095 param = typeof param == "string" ? param.replace(/,/g, '|') : "js"; 1096 return this.optional(element) || value.match(new RegExp(".(" + param + ")$", "i")); 1097 }, 1098 1099 // http://docs.jquery.com/Plugins/Validation/Methods/equalTo 1100 equalTo: function(value, element, param) { 1101 // bind to the blur event of the target in order to revalidate whenever the target field is updated 1102 // TODO find a way to bind the event just once, avoiding the unbind-rebind overhead 1103 var target = $(param).unbind(".validate-equalTo").bind("blur.validate-equalTo", function() { 1104 $(element).valid(); 1105 }); 1106 return value == target.val(); 1107 } 1108 1109 } 1110 1111}); 1112 1113// deprecated, use $.validator.format instead 1114$.format = $.validator.format; 1115 1116})(jQuery); 1117 1118// ajax mode: abort 1119// usage: $.ajax({ mode: "abort"[, port: "uniqueport"]}); 1120// if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort() 1121;(function($) { 1122 var pendingRequests = {}; 1123 // Use a prefilter if available (1.5+) 1124 if ( $.ajaxPrefilter ) { 1125 $.ajaxPrefilter(function(settings, _, xhr) { 1126 var port = settings.port; 1127 if (settings.mode == "abort") { 1128 if ( pendingRequests[port] ) { 1129 pendingRequests[port].abort(); 1130 } 1131 pendingRequests[port] = xhr; 1132 } 1133 }); 1134 } else { 1135 // Proxy ajax 1136 var ajax = $.ajax; 1137 $.ajax = function(settings) { 1138 var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode, 1139 port = ( "port" in settings ? settings : $.ajaxSettings ).port; 1140 if (mode == "abort") { 1141 if ( pendingRequests[port] ) { 1142 pendingRequests[port].abort(); 1143 } 1144 return (pendingRequests[port] = ajax.apply(this, arguments)); 1145 } 1146 return ajax.apply(this, arguments); 1147 }; 1148 } 1149})(jQuery); 1150 1151// provides cross-browser focusin and focusout events 1152// IE has native support, in other browsers, use event caputuring (neither bubbles) 1153 1154// provides delegate(type: String, delegate: Selector, handler: Callback) plugin for easier event delegation 1155// handler is only called when $(event.target).is(delegate), in the scope of the jquery-object for event.target 1156;(function($) { 1157 // only implement if not provided by jQuery core (since 1.4) 1158 // TODO verify if jQuery 1.4's implementation is compatible with older jQuery special-event APIs 1159 if (!jQuery.event.special.focusin && !jQuery.event.special.focusout && document.addEventListener) { 1160 $.each({ 1161 focus: 'focusin', 1162 blur: 'focusout' 1163 }, function( original, fix ){ 1164 $.event.special[fix] = { 1165 setup:function() { 1166 this.addEventListener( original, handler, true ); 1167 }, 1168 teardown:function() { 1169 this.removeEventListener( original, handler, true ); 1170 }, 1171 handler: function(e) { 1172 arguments[0] = $.event.fix(e); 1173 arguments[0].type = fix; 1174 return $.event.handle.apply(this, arguments); 1175 } 1176 }; 1177 function handler(e) { 1178 e = $.event.fix(e); 1179 e.type = fix; 1180 return $.event.handle.call(this, e); 1181 } 1182 }); 1183 }; 1184 $.extend($.fn, { 1185 validateDelegate: function(delegate, type, handler) { 1186 return this.bind(type, function(event) { 1187 var target = $(event.target); 1188 if (target.is(delegate)) { 1189 return handler.apply(target, arguments); 1190 } 1191 }); 1192 } 1193 }); 1194})(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.