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