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