comparison Agendas/trunk/src/Agendas.Web/Scripts/jquery.validate-vsdoc.js @ 10:c62b77fc33f4

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