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