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