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