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