1 /* NUGET: BEGIN LICENSE TEXT
3 * Microsoft grants you the right to use these script files for the sole
4 * purpose of either: (i) interacting through your browser with the Microsoft
5 * website or online service, subject to the applicable licensing or use
6 * terms; or (ii) using the files as included with a Microsoft product subject
7 * to that product's license terms. Microsoft reserves all other rights to the
8 * files not expressly granted by Microsoft, whether by implication, estoppel
9 * or otherwise. Insofar as a script file is dual licensed under GPL,
10 * Microsoft neither took the code under GPL nor distributes it thereunder but
11 * under the terms set out in this paragraph. All notices and licenses
12 * below are for informational purposes only.
14 * NUGET: END LICENSE TEXT */
16 * jQuery Validation Plugin 1.11.1
18 * http://bassistance.de/jquery-plugins/jquery-plugin-validation/
19 * http://docs.jquery.com/Plugins/Validation
21 * Copyright 2013 Jörn Zaefferer
22 * Released under the MIT license:
23 * http://www.opensource.org/licenses/mit-license.php
29 // http://docs.jquery.com/Plugins/Validation/validate
30 validate: function( options ) {
32 // if nothing is selected, return nothing; can't chain anyway
34 if ( options && options.debug && window.console ) {
35 console.warn( "Nothing selected, can't validate, returning nothing." );
40 // check if a validator for this form was already created
41 var validator = $.data( this[0], "validator" );
46 // Add novalidate tag if HTML5.
47 this.attr( "novalidate", "novalidate" );
49 validator = new $.validator( options, this[0] );
50 $.data( this[0], "validator", validator );
52 if ( validator.settings.onsubmit ) {
54 this.validateDelegate( ":submit", "click", function( event ) {
55 if ( validator.settings.submitHandler ) {
56 validator.submitButton = event.target;
58 // allow suppressing validation by adding a cancel class to the submit button
59 if ( $(event.target).hasClass("cancel") ) {
60 validator.cancelSubmit = true;
63 // allow suppressing validation by adding the html5 formnovalidate attribute to the submit button
64 if ( $(event.target).attr("formnovalidate") !== undefined ) {
65 validator.cancelSubmit = true;
69 // validate the form on submit
70 this.submit( function( event ) {
71 if ( validator.settings.debug ) {
72 // prevent form submit to be able to see console output
73 event.preventDefault();
77 if ( validator.settings.submitHandler ) {
78 if ( validator.submitButton ) {
79 // insert a hidden input as a replacement for the missing submit button
80 hidden = $("<input type='hidden'/>").attr("name", validator.submitButton.name).val( $(validator.submitButton).val() ).appendTo(validator.currentForm);
82 validator.settings.submitHandler.call( validator, validator.currentForm, event );
83 if ( validator.submitButton ) {
84 // and clean up afterwards; thanks to no-block-scope, hidden can be referenced
92 // prevent submit for invalid forms or custom submit handlers
93 if ( validator.cancelSubmit ) {
94 validator.cancelSubmit = false;
97 if ( validator.form() ) {
98 if ( validator.pendingRequest ) {
99 validator.formSubmitted = true;
104 validator.focusInvalid();
112 // http://docs.jquery.com/Plugins/Validation/valid
114 if ( $(this[0]).is("form")) {
115 return this.validate().form();
118 var validator = $(this[0].form).validate();
119 this.each(function() {
120 valid = valid && validator.element(this);
125 // attributes: space seperated list of attributes to retrieve and remove
126 removeAttrs: function( attributes ) {
129 $.each(attributes.split(/\s/), function( index, value ) {
130 result[value] = $element.attr(value);
131 $element.removeAttr(value);
135 // http://docs.jquery.com/Plugins/Validation/rules
136 rules: function( command, argument ) {
137 var element = this[0];
140 var settings = $.data(element.form, "validator").settings;
141 var staticRules = settings.rules;
142 var existingRules = $.validator.staticRules(element);
145 $.extend(existingRules, $.validator.normalizeRule(argument));
146 // remove messages from rules, but allow them to be set separetely
147 delete existingRules.messages;
148 staticRules[element.name] = existingRules;
149 if ( argument.messages ) {
150 settings.messages[element.name] = $.extend( settings.messages[element.name], argument.messages );
155 delete staticRules[element.name];
156 return existingRules;
159 $.each(argument.split(/\s/), function( index, method ) {
160 filtered[method] = existingRules[method];
161 delete existingRules[method];
167 var data = $.validator.normalizeRules(
170 $.validator.classRules(element),
171 $.validator.attributeRules(element),
172 $.validator.dataRules(element),
173 $.validator.staticRules(element)
176 // make sure required is at front
177 if ( data.required ) {
178 var param = data.required;
179 delete data.required;
180 data = $.extend({required: param}, data);
188 $.extend($.expr[":"], {
189 // http://docs.jquery.com/Plugins/Validation/blank
190 blank: function( a ) { return !$.trim("" + $(a).val()); },
191 // http://docs.jquery.com/Plugins/Validation/filled
192 filled: function( a ) { return !!$.trim("" + $(a).val()); },
193 // http://docs.jquery.com/Plugins/Validation/unchecked
194 unchecked: function( a ) { return !$(a).prop("checked"); }
197 // constructor for validator
198 $.validator = function( options, form ) {
199 this.settings = $.extend( true, {}, $.validator.defaults, options );
200 this.currentForm = form;
204 $.validator.format = function( source, params ) {
205 if ( arguments.length === 1 ) {
207 var args = $.makeArray(arguments);
208 args.unshift(source);
209 return $.validator.format.apply( this, args );
212 if ( arguments.length > 2 && params.constructor !== Array ) {
213 params = $.makeArray(arguments).slice(1);
215 if ( params.constructor !== Array ) {
218 $.each(params, function( i, n ) {
219 source = source.replace( new RegExp("\\{" + i + "\\}", "g"), function() {
226 $.extend($.validator, {
234 errorElement: "label",
236 errorContainer: $([]),
237 errorLabelContainer: $([]),
241 onfocusin: function( element, event ) {
242 this.lastActive = element;
244 // hide error label and remove error class on focus if enabled
245 if ( this.settings.focusCleanup && !this.blockFocusCleanup ) {
246 if ( this.settings.unhighlight ) {
247 this.settings.unhighlight.call( this, element, this.settings.errorClass, this.settings.validClass );
249 this.addWrapper(this.errorsFor(element)).hide();
252 onfocusout: function( element, event ) {
253 if ( !this.checkable(element) && (element.name in this.submitted || !this.optional(element)) ) {
254 this.element(element);
257 onkeyup: function( element, event ) {
258 if ( event.which === 9 && this.elementValue(element) === "" ) {
260 } else if ( element.name in this.submitted || element === this.lastElement ) {
261 this.element(element);
264 onclick: function( element, event ) {
265 // click on selects, radiobuttons and checkboxes
266 if ( element.name in this.submitted ) {
267 this.element(element);
269 // or option elements, check parent select in that case
270 else if ( element.parentNode.name in this.submitted ) {
271 this.element(element.parentNode);
274 highlight: function( element, errorClass, validClass ) {
275 if ( element.type === "radio" ) {
276 this.findByName(element.name).addClass(errorClass).removeClass(validClass);
278 $(element).addClass(errorClass).removeClass(validClass);
281 unhighlight: function( element, errorClass, validClass ) {
282 if ( element.type === "radio" ) {
283 this.findByName(element.name).removeClass(errorClass).addClass(validClass);
285 $(element).removeClass(errorClass).addClass(validClass);
290 // http://docs.jquery.com/Plugins/Validation/Validator/setDefaults
291 setDefaults: function( settings ) {
292 $.extend( $.validator.defaults, settings );
296 required: "This field is required.",
297 remote: "Please fix this field.",
298 email: "Please enter a valid email address.",
299 url: "Please enter a valid URL.",
300 date: "Please enter a valid date.",
301 dateISO: "Please enter a valid date (ISO).",
302 number: "Please enter a valid number.",
303 digits: "Please enter only digits.",
304 creditcard: "Please enter a valid credit card number.",
305 equalTo: "Please enter the same value again.",
306 maxlength: $.validator.format("Please enter no more than {0} characters."),
307 minlength: $.validator.format("Please enter at least {0} characters."),
308 rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
309 range: $.validator.format("Please enter a value between {0} and {1}."),
310 max: $.validator.format("Please enter a value less than or equal to {0}."),
311 min: $.validator.format("Please enter a value greater than or equal to {0}.")
314 autoCreateRanges: false,
319 this.labelContainer = $(this.settings.errorLabelContainer);
320 this.errorContext = this.labelContainer.length && this.labelContainer || $(this.currentForm);
321 this.containers = $(this.settings.errorContainer).add( this.settings.errorLabelContainer );
323 this.valueCache = {};
324 this.pendingRequest = 0;
329 var groups = (this.groups = {});
330 $.each(this.settings.groups, function( key, value ) {
331 if ( typeof value === "string" ) {
332 value = value.split(/\s/);
334 $.each(value, function( index, name ) {
338 var rules = this.settings.rules;
339 $.each(rules, function( key, value ) {
340 rules[key] = $.validator.normalizeRule(value);
343 function delegate(event) {
344 var validator = $.data(this[0].form, "validator"),
345 eventType = "on" + event.type.replace(/^validate/, "");
346 if ( validator.settings[eventType] ) {
347 validator.settings[eventType].call(validator, this[0], event);
351 .validateDelegate(":text, [type='password'], [type='file'], select, textarea, " +
352 "[type='number'], [type='search'] ,[type='tel'], [type='url'], " +
353 "[type='email'], [type='datetime'], [type='date'], [type='month'], " +
354 "[type='week'], [type='time'], [type='datetime-local'], " +
355 "[type='range'], [type='color'] ",
356 "focusin focusout keyup", delegate)
357 .validateDelegate("[type='radio'], [type='checkbox'], select, option", "click", delegate);
359 if ( this.settings.invalidHandler ) {
360 $(this.currentForm).bind("invalid-form.validate", this.settings.invalidHandler);
364 // http://docs.jquery.com/Plugins/Validation/Validator/form
367 $.extend(this.submitted, this.errorMap);
368 this.invalid = $.extend({}, this.errorMap);
369 if ( !this.valid() ) {
370 $(this.currentForm).triggerHandler("invalid-form", [this]);
376 checkForm: function() {
378 for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
379 this.check( elements[i] );
384 // http://docs.jquery.com/Plugins/Validation/Validator/element
385 element: function( element ) {
386 element = this.validationTargetFor( this.clean( element ) );
387 this.lastElement = element;
388 this.prepareElement( element );
389 this.currentElements = $(element);
390 var result = this.check( element ) !== false;
392 delete this.invalid[element.name];
394 this.invalid[element.name] = true;
396 if ( !this.numberOfInvalids() ) {
397 // Hide error containers on last error
398 this.toHide = this.toHide.add( this.containers );
404 // http://docs.jquery.com/Plugins/Validation/Validator/showErrors
405 showErrors: function( errors ) {
407 // add items to error list and map
408 $.extend( this.errorMap, errors );
410 for ( var name in errors ) {
411 this.errorList.push({
412 message: errors[name],
413 element: this.findByName(name)[0]
416 // remove items from success list
417 this.successList = $.grep( this.successList, function( element ) {
418 return !(element.name in errors);
421 if ( this.settings.showErrors ) {
422 this.settings.showErrors.call( this, this.errorMap, this.errorList );
424 this.defaultShowErrors();
428 // http://docs.jquery.com/Plugins/Validation/Validator/resetForm
429 resetForm: function() {
430 if ( $.fn.resetForm ) {
431 $(this.currentForm).resetForm();
434 this.lastElement = null;
437 this.elements().removeClass( this.settings.errorClass ).removeData( "previousValue" );
440 numberOfInvalids: function() {
441 return this.objectLength(this.invalid);
444 objectLength: function( obj ) {
446 for ( var i in obj ) {
452 hideErrors: function() {
453 this.addWrapper( this.toHide ).hide();
457 return this.size() === 0;
461 return this.errorList.length;
464 focusInvalid: function() {
465 if ( this.settings.focusInvalid ) {
467 $(this.findLastActive() || this.errorList.length && this.errorList[0].element || [])
470 // manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find
473 // ignore IE throwing errors when focusing hidden elements
478 findLastActive: function() {
479 var lastActive = this.lastActive;
480 return lastActive && $.grep(this.errorList, function( n ) {
481 return n.element.name === lastActive.name;
482 }).length === 1 && lastActive;
485 elements: function() {
486 var validator = this,
489 // select all valid inputs inside the form (no submit or reset buttons)
490 return $(this.currentForm)
491 .find("input, select, textarea")
492 .not(":submit, :reset, :image, [disabled]")
493 .not( this.settings.ignore )
495 if ( !this.name && validator.settings.debug && window.console ) {
496 console.error( "%o has no name assigned", this);
499 // select only the first element for each name, and only those with rules specified
500 if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) {
504 rulesCache[this.name] = true;
509 clean: function( selector ) {
510 return $(selector)[0];
514 var errorClass = this.settings.errorClass.replace(" ", ".");
515 return $(this.settings.errorElement + "." + errorClass, this.errorContext);
519 this.successList = [];
524 this.currentElements = $([]);
527 prepareForm: function() {
529 this.toHide = this.errors().add( this.containers );
532 prepareElement: function( element ) {
534 this.toHide = this.errorsFor(element);
537 elementValue: function( element ) {
538 var type = $(element).attr("type"),
539 val = $(element).val();
541 if ( type === "radio" || type === "checkbox" ) {
542 return $("input[name='" + $(element).attr("name") + "']:checked").val();
545 if ( typeof val === "string" ) {
546 return val.replace(/\r/g, "");
551 check: function( element ) {
552 element = this.validationTargetFor( this.clean( element ) );
554 var rules = $(element).rules();
555 var dependencyMismatch = false;
556 var val = this.elementValue(element);
559 for (var method in rules ) {
560 var rule = { method: method, parameters: rules[method] };
563 result = $.validator.methods[method].call( this, val, element, rule.parameters );
565 // if a method indicates that the field is optional and therefore valid,
566 // don't mark it as valid when there are no other rules
567 if ( result === "dependency-mismatch" ) {
568 dependencyMismatch = true;
571 dependencyMismatch = false;
573 if ( result === "pending" ) {
574 this.toHide = this.toHide.not( this.errorsFor(element) );
579 this.formatAndAdd( element, rule );
583 if ( this.settings.debug && window.console ) {
584 console.log( "Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.", e );
589 if ( dependencyMismatch ) {
592 if ( this.objectLength(rules) ) {
593 this.successList.push(element);
598 // return the custom message for the given element and validation method
599 // specified in the element's HTML5 data attribute
600 customDataMessage: function( element, method ) {
601 return $(element).data("msg-" + method.toLowerCase()) || (element.attributes && $(element).attr("data-msg-" + method.toLowerCase()));
604 // return the custom message for the given element name and validation method
605 customMessage: function( name, method ) {
606 var m = this.settings.messages[name];
607 return m && (m.constructor === String ? m : m[method]);
610 // return the first defined argument, allowing empty strings
611 findDefined: function() {
612 for(var i = 0; i < arguments.length; i++) {
613 if ( arguments[i] !== undefined ) {
620 defaultMessage: function( element, method ) {
621 return this.findDefined(
622 this.customMessage( element.name, method ),
623 this.customDataMessage( element, method ),
624 // title is never undefined, so handle empty string as undefined
625 !this.settings.ignoreTitle && element.title || undefined,
626 $.validator.messages[method],
627 "<strong>Warning: No message defined for " + element.name + "</strong>"
631 formatAndAdd: function( element, rule ) {
632 var message = this.defaultMessage( element, rule.method ),
633 theregex = /\$?\{(\d+)\}/g;
634 if ( typeof message === "function" ) {
635 message = message.call(this, rule.parameters, element);
636 } else if (theregex.test(message)) {
637 message = $.validator.format(message.replace(theregex, "{$1}"), rule.parameters);
639 this.errorList.push({
644 this.errorMap[element.name] = message;
645 this.submitted[element.name] = message;
648 addWrapper: function( toToggle ) {
649 if ( this.settings.wrapper ) {
650 toToggle = toToggle.add( toToggle.parent( this.settings.wrapper ) );
655 defaultShowErrors: function() {
657 for ( i = 0; this.errorList[i]; i++ ) {
658 var error = this.errorList[i];
659 if ( this.settings.highlight ) {
660 this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass );
662 this.showLabel( error.element, error.message );
664 if ( this.errorList.length ) {
665 this.toShow = this.toShow.add( this.containers );
667 if ( this.settings.success ) {
668 for ( i = 0; this.successList[i]; i++ ) {
669 this.showLabel( this.successList[i] );
672 if ( this.settings.unhighlight ) {
673 for ( i = 0, elements = this.validElements(); elements[i]; i++ ) {
674 this.settings.unhighlight.call( this, elements[i], this.settings.errorClass, this.settings.validClass );
677 this.toHide = this.toHide.not( this.toShow );
679 this.addWrapper( this.toShow ).show();
682 validElements: function() {
683 return this.currentElements.not(this.invalidElements());
686 invalidElements: function() {
687 return $(this.errorList).map(function() {
692 showLabel: function( element, message ) {
693 var label = this.errorsFor( element );
694 if ( label.length ) {
695 // refresh error/success class
696 label.removeClass( this.settings.validClass ).addClass( this.settings.errorClass );
697 // replace message on existing label
701 label = $("<" + this.settings.errorElement + ">")
702 .attr("for", this.idOrName(element))
703 .addClass(this.settings.errorClass)
704 .html(message || "");
705 if ( this.settings.wrapper ) {
706 // make sure the element is visible, even in IE
707 // actually showing the wrapped element is handled elsewhere
708 label = label.hide().show().wrap("<" + this.settings.wrapper + "/>").parent();
710 if ( !this.labelContainer.append(label).length ) {
711 if ( this.settings.errorPlacement ) {
712 this.settings.errorPlacement(label, $(element) );
714 label.insertAfter(element);
718 if ( !message && this.settings.success ) {
720 if ( typeof this.settings.success === "string" ) {
721 label.addClass( this.settings.success );
723 this.settings.success( label, element );
726 this.toShow = this.toShow.add(label);
729 errorsFor: function( element ) {
730 var name = this.idOrName(element);
731 return this.errors().filter(function() {
732 return $(this).attr("for") === name;
736 idOrName: function( element ) {
737 return this.groups[element.name] || (this.checkable(element) ? element.name : element.id || element.name);
740 validationTargetFor: function( element ) {
741 // if radio/checkbox, validate first element in group instead
742 if ( this.checkable(element) ) {
743 element = this.findByName( element.name ).not(this.settings.ignore)[0];
748 checkable: function( element ) {
749 return (/radio|checkbox/i).test(element.type);
752 findByName: function( name ) {
753 return $(this.currentForm).find("[name='" + name + "']");
756 getLength: function( value, element ) {
757 switch( element.nodeName.toLowerCase() ) {
759 return $("option:selected", element).length;
761 if ( this.checkable( element) ) {
762 return this.findByName(element.name).filter(":checked").length;
768 depend: function( param, element ) {
769 return this.dependTypes[typeof param] ? this.dependTypes[typeof param](param, element) : true;
773 "boolean": function( param, element ) {
776 "string": function( param, element ) {
777 return !!$(param, element.form).length;
779 "function": function( param, element ) {
780 return param(element);
784 optional: function( element ) {
785 var val = this.elementValue(element);
786 return !$.validator.methods.required.call(this, val, element) && "dependency-mismatch";
789 startRequest: function( element ) {
790 if ( !this.pending[element.name] ) {
791 this.pendingRequest++;
792 this.pending[element.name] = true;
796 stopRequest: function( element, valid ) {
797 this.pendingRequest--;
798 // sometimes synchronization fails, make sure pendingRequest is never < 0
799 if ( this.pendingRequest < 0 ) {
800 this.pendingRequest = 0;
802 delete this.pending[element.name];
803 if ( valid && this.pendingRequest === 0 && this.formSubmitted && this.form() ) {
804 $(this.currentForm).submit();
805 this.formSubmitted = false;
806 } else if (!valid && this.pendingRequest === 0 && this.formSubmitted) {
807 $(this.currentForm).triggerHandler("invalid-form", [this]);
808 this.formSubmitted = false;
812 previousValue: function( element ) {
813 return $.data(element, "previousValue") || $.data(element, "previousValue", {
816 message: this.defaultMessage( element, "remote" )
823 required: {required: true},
824 email: {email: true},
827 dateISO: {dateISO: true},
828 number: {number: true},
829 digits: {digits: true},
830 creditcard: {creditcard: true}
833 addClassRules: function( className, rules ) {
834 if ( className.constructor === String ) {
835 this.classRuleSettings[className] = rules;
837 $.extend(this.classRuleSettings, className);
841 classRules: function( element ) {
843 var classes = $(element).attr("class");
845 $.each(classes.split(" "), function() {
846 if ( this in $.validator.classRuleSettings ) {
847 $.extend(rules, $.validator.classRuleSettings[this]);
854 attributeRules: function( element ) {
856 var $element = $(element);
857 var type = $element[0].getAttribute("type");
859 for (var method in $.validator.methods) {
862 // support for <input required> in both html5 and older browsers
863 if ( method === "required" ) {
864 value = $element.get(0).getAttribute(method);
865 // Some browsers return an empty string for the required attribute
866 // and non-HTML5 browsers might have required="" markup
867 if ( value === "" ) {
870 // force non-HTML5 browsers to return bool
873 value = $element.attr(method);
876 // convert the value to a number for number inputs, and for text for backwards compability
877 // allows type="date" and others to be compared as strings
878 if ( /min|max/.test( method ) && ( type === null || /number|range|text/.test( type ) ) ) {
879 value = Number(value);
883 rules[method] = value;
884 } else if ( type === method && type !== 'range' ) {
885 // exception: the jquery validate 'range' method
886 // does not test for the html5 'range' type
887 rules[method] = true;
891 // maxlength may be returned as -1, 2147483647 (IE) and 524288 (safari) for text inputs
892 if ( rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength) ) {
893 delete rules.maxlength;
899 dataRules: function( element ) {
901 rules = {}, $element = $(element);
902 for (method in $.validator.methods) {
903 value = $element.data("rule-" + method.toLowerCase());
904 if ( value !== undefined ) {
905 rules[method] = value;
911 staticRules: function( element ) {
913 var validator = $.data(element.form, "validator");
914 if ( validator.settings.rules ) {
915 rules = $.validator.normalizeRule(validator.settings.rules[element.name]) || {};
920 normalizeRules: function( rules, element ) {
921 // handle dependency check
922 $.each(rules, function( prop, val ) {
923 // ignore rule when param is explicitly false, eg. required:false
924 if ( val === false ) {
928 if ( val.param || val.depends ) {
930 switch (typeof val.depends) {
932 keepRule = !!$(val.depends, element.form).length;
935 keepRule = val.depends.call(element, element);
939 rules[prop] = val.param !== undefined ? val.param : true;
946 // evaluate parameters
947 $.each(rules, function( rule, parameter ) {
948 rules[rule] = $.isFunction(parameter) ? parameter(element) : parameter;
951 // clean number parameters
952 $.each(['minlength', 'maxlength'], function() {
954 rules[this] = Number(rules[this]);
957 $.each(['rangelength', 'range'], function() {
960 if ( $.isArray(rules[this]) ) {
961 rules[this] = [Number(rules[this][0]), Number(rules[this][1])];
962 } else if ( typeof rules[this] === "string" ) {
963 parts = rules[this].split(/[\s,]+/);
964 rules[this] = [Number(parts[0]), Number(parts[1])];
969 if ( $.validator.autoCreateRanges ) {
970 // auto-create ranges
971 if ( rules.min && rules.max ) {
972 rules.range = [rules.min, rules.max];
976 if ( rules.minlength && rules.maxlength ) {
977 rules.rangelength = [rules.minlength, rules.maxlength];
978 delete rules.minlength;
979 delete rules.maxlength;
986 // Converts a simple string to a {string: true} rule, e.g., "required" to {required:true}
987 normalizeRule: function( data ) {
988 if ( typeof data === "string" ) {
989 var transformed = {};
990 $.each(data.split(/\s/), function() {
991 transformed[this] = true;
998 // http://docs.jquery.com/Plugins/Validation/Validator/addMethod
999 addMethod: function( name, method, message ) {
1000 $.validator.methods[name] = method;
1001 $.validator.messages[name] = message !== undefined ? message : $.validator.messages[name];
1002 if ( method.length < 3 ) {
1003 $.validator.addClassRules(name, $.validator.normalizeRule(name));
1009 // http://docs.jquery.com/Plugins/Validation/Methods/required
1010 required: function( value, element, param ) {
1011 // check if dependency is met
1012 if ( !this.depend(param, element) ) {
1013 return "dependency-mismatch";
1015 if ( element.nodeName.toLowerCase() === "select" ) {
1016 // could be an array for select-multiple or a string, both are fine this way
1017 var val = $(element).val();
1018 return val && val.length > 0;
1020 if ( this.checkable(element) ) {
1021 return this.getLength(value, element) > 0;
1023 return $.trim(value).length > 0;
1026 // http://docs.jquery.com/Plugins/Validation/Methods/email
1027 email: function( value, element ) {
1028 // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
1029 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);
1032 // http://docs.jquery.com/Plugins/Validation/Methods/url
1033 url: function( value, element ) {
1034 // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
1035 return this.optional(element) || /^(https?|s?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);
1038 // http://docs.jquery.com/Plugins/Validation/Methods/date
1039 date: function( value, element ) {
1040 return this.optional(element) || !/Invalid|NaN/.test(new Date(value).toString());
1043 // http://docs.jquery.com/Plugins/Validation/Methods/dateISO
1044 dateISO: function( value, element ) {
1045 return this.optional(element) || /^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/.test(value);
1048 // http://docs.jquery.com/Plugins/Validation/Methods/number
1049 number: function( value, element ) {
1050 return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
1053 // http://docs.jquery.com/Plugins/Validation/Methods/digits
1054 digits: function( value, element ) {
1055 return this.optional(element) || /^\d+$/.test(value);
1058 // http://docs.jquery.com/Plugins/Validation/Methods/creditcard
1059 // based on http://en.wikipedia.org/wiki/Luhn
1060 creditcard: function( value, element ) {
1061 if ( this.optional(element) ) {
1062 return "dependency-mismatch";
1064 // accept only spaces, digits and dashes
1065 if ( /[^0-9 \-]+/.test(value) ) {
1072 value = value.replace(/\D/g, "");
1074 for (var n = value.length - 1; n >= 0; n--) {
1075 var cDigit = value.charAt(n);
1076 nDigit = parseInt(cDigit, 10);
1078 if ( (nDigit *= 2) > 9 ) {
1086 return (nCheck % 10) === 0;
1089 // http://docs.jquery.com/Plugins/Validation/Methods/minlength
1090 minlength: function( value, element, param ) {
1091 var length = $.isArray( value ) ? value.length : this.getLength($.trim(value), element);
1092 return this.optional(element) || length >= param;
1095 // http://docs.jquery.com/Plugins/Validation/Methods/maxlength
1096 maxlength: function( value, element, param ) {
1097 var length = $.isArray( value ) ? value.length : this.getLength($.trim(value), element);
1098 return this.optional(element) || length <= param;
1101 // http://docs.jquery.com/Plugins/Validation/Methods/rangelength
1102 rangelength: function( value, element, param ) {
1103 var length = $.isArray( value ) ? value.length : this.getLength($.trim(value), element);
1104 return this.optional(element) || ( length >= param[0] && length <= param[1] );
1107 // http://docs.jquery.com/Plugins/Validation/Methods/min
1108 min: function( value, element, param ) {
1109 return this.optional(element) || value >= param;
1112 // http://docs.jquery.com/Plugins/Validation/Methods/max
1113 max: function( value, element, param ) {
1114 return this.optional(element) || value <= param;
1117 // http://docs.jquery.com/Plugins/Validation/Methods/range
1118 range: function( value, element, param ) {
1119 return this.optional(element) || ( value >= param[0] && value <= param[1] );
1122 // http://docs.jquery.com/Plugins/Validation/Methods/equalTo
1123 equalTo: function( value, element, param ) {
1124 // bind to the blur event of the target in order to revalidate whenever the target field is updated
1125 // TODO find a way to bind the event just once, avoiding the unbind-rebind overhead
1126 var target = $(param);
1127 if ( this.settings.onfocusout ) {
1128 target.unbind(".validate-equalTo").bind("blur.validate-equalTo", function() {
1132 return value === target.val();
1135 // http://docs.jquery.com/Plugins/Validation/Methods/remote
1136 remote: function( value, element, param ) {
1137 if ( this.optional(element) ) {
1138 return "dependency-mismatch";
1141 var previous = this.previousValue(element);
1142 if (!this.settings.messages[element.name] ) {
1143 this.settings.messages[element.name] = {};
1145 previous.originalMessage = this.settings.messages[element.name].remote;
1146 this.settings.messages[element.name].remote = previous.message;
1148 param = typeof param === "string" && {url:param} || param;
1150 if ( previous.old === value ) {
1151 return previous.valid;
1154 previous.old = value;
1155 var validator = this;
1156 this.startRequest(element);
1158 data[element.name] = value;
1159 $.ajax($.extend(true, {
1162 port: "validate" + element.name,
1165 success: function( response ) {
1166 validator.settings.messages[element.name].remote = previous.originalMessage;
1167 var valid = response === true || response === "true";
1169 var submitted = validator.formSubmitted;
1170 validator.prepareElement(element);
1171 validator.formSubmitted = submitted;
1172 validator.successList.push(element);
1173 delete validator.invalid[element.name];
1174 validator.showErrors();
1177 var message = response || validator.defaultMessage( element, "remote" );
1178 errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
1179 validator.invalid[element.name] = true;
1180 validator.showErrors(errors);
1182 previous.valid = valid;
1183 validator.stopRequest(element, valid);
1193 // deprecated, use $.validator.format instead
1194 $.format = $.validator.format;
1199 // usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
1200 // if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort()
1202 var pendingRequests = {};
1203 // Use a prefilter if available (1.5+)
1204 if ( $.ajaxPrefilter ) {
1205 $.ajaxPrefilter(function( settings, _, xhr ) {
1206 var port = settings.port;
1207 if ( settings.mode === "abort" ) {
1208 if ( pendingRequests[port] ) {
1209 pendingRequests[port].abort();
1211 pendingRequests[port] = xhr;
1217 $.ajax = function( settings ) {
1218 var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode,
1219 port = ( "port" in settings ? settings : $.ajaxSettings ).port;
1220 if ( mode === "abort" ) {
1221 if ( pendingRequests[port] ) {
1222 pendingRequests[port].abort();
1224 pendingRequests[port] = ajax.apply(this, arguments);
1225 return pendingRequests[port];
1227 return ajax.apply(this, arguments);
1232 // provides delegate(type: String, delegate: Selector, handler: Callback) plugin for easier event delegation
1233 // handler is only called when $(event.target).is(delegate), in the scope of the jquery-object for event.target
1236 validateDelegate: function( delegate, type, handler ) {
1237 return this.bind(type, function( event ) {
1238 var target = $(event.target);
1239 if ( target.is(delegate) ) {
1240 return handler.apply(target, arguments);