jquery.datepick.validation.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* http://keith-wood.name/datepick.html
  2. Datepicker Validation extension for jQuery 4.0.6.
  3. Requires Jörn Zaefferer's Validation plugin (http://plugins.jquery.com/project/validate).
  4. Written by Keith Wood (kbwood{at}iinet.com.au).
  5. Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
  6. MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
  7. Please attribute the author if you use it. */
  8. (function($) { // Hide the namespace
  9. /* Add validation methods if validation plugin available. */
  10. if ($.fn.validate) {
  11. $.datepick.selectDateOrig = $.datepick.selectDate;
  12. $.extend($.datepick.regional[''], {
  13. validateDate: 'Please enter a valid date',
  14. validateDateMin: 'Please enter a date on or after {0}',
  15. validateDateMax: 'Please enter a date on or before {0}',
  16. validateDateMinMax: 'Please enter a date between {0} and {1}',
  17. validateDateCompare: 'Please enter a date {0} {1}',
  18. validateDateToday: 'today',
  19. validateDateOther: 'the other date',
  20. validateDateEQ: 'equal to',
  21. validateDateNE: 'not equal to',
  22. validateDateLT: 'before',
  23. validateDateGT: 'after',
  24. validateDateLE: 'not after',
  25. validateDateGE: 'not before'
  26. });
  27. $.extend($.datepick._defaults, $.datepick.regional['']);
  28. $.extend($.datepick, {
  29. /* Trigger a validation after updating the input field with the selected date.
  30. @param target (element) the control to examine
  31. @param elem (element) the selected datepicker element */
  32. selectDate: function(target, elem) {
  33. this.selectDateOrig(target, elem);
  34. var inst = $.data(target, $.datepick.dataName);
  35. if (!inst.inline && $.fn.validate) {
  36. var validation = $(target).parents('form').validate();
  37. if (validation) {
  38. validation.element('#' + target.id);
  39. }
  40. }
  41. },
  42. /* Correct error placement for validation errors - after any trigger.
  43. @param error (jQuery) the error message
  44. @param element (jQuery) the field in error */
  45. errorPlacement: function(error, element) {
  46. var inst = $.data(element[0], $.datepick.dataName);
  47. if (inst) {
  48. error[inst.get('isRTL') ? 'insertBefore' : 'insertAfter'](
  49. inst.trigger.length > 0 ? inst.trigger : element);
  50. }
  51. else {
  52. error.insertAfter(element);
  53. }
  54. },
  55. /* Format a validation error message involving dates.
  56. @param source (string) the error message
  57. @param params (Date[]) the dates
  58. @return (string) the formatted message */
  59. errorFormat: function(source, params) {
  60. var format = ($.datepick.curInst ?
  61. $.datepick.curInst.get('dateFormat') :
  62. $.datepick._defaults.dateFormat);
  63. $.each(params, function(index, value) {
  64. source = source.replace(new RegExp('\\{' + index + '\\}', 'g'),
  65. $.datepick.formatDate(format, value) || 'nothing');
  66. });
  67. return source;
  68. }
  69. });
  70. var lastElement = null;
  71. /* Validate date field. */
  72. $.validator.addMethod('dpDate', function(value, element) {
  73. lastElement = element;
  74. return this.optional(element) || validateEach(value, element);
  75. },
  76. function(params) {
  77. var inst = $.data(lastElement, $.datepick.dataName);
  78. var minDate = inst.get('minDate');
  79. var maxDate = inst.get('maxDate');
  80. var messages = $.datepick._defaults;
  81. return (minDate && maxDate ?
  82. $.datepick.errorFormat(messages.validateDateMinMax, [minDate, maxDate]) :
  83. (minDate ? $.datepick.errorFormat(messages.validateDateMin, [minDate]) :
  84. (maxDate ? $.datepick.errorFormat(messages.validateDateMax, [maxDate]) :
  85. messages.validateDate)));
  86. });
  87. /* Apply a validation test to each date provided.
  88. @param value (string) the current field value
  89. @param element (element) the field control
  90. @return (boolean) true if OK, false if failed validation */
  91. function validateEach(value, element) {
  92. var inst = $.data(element, $.datepick.dataName);
  93. var rangeSelect = inst.get('rangeSelect');
  94. var multiSelect = inst.get('multiSelect');
  95. var dates = (multiSelect ? value.split(inst.get('multiSeparator')) :
  96. (rangeSelect ? value.split(inst.get('rangeSeparator')) : [value]));
  97. var ok = (multiSelect && dates.length <= multiSelect) ||
  98. (!multiSelect && rangeSelect && dates.length == 2) ||
  99. (!multiSelect && !rangeSelect && dates.length == 1);
  100. if (ok) {
  101. try {
  102. var dateFormat = inst.get('dateFormat');
  103. var minDate = inst.get('minDate');
  104. var maxDate = inst.get('maxDate');
  105. var dp = $(element);
  106. $.each(dates, function(i, v) {
  107. dates[i] = $.datepick.parseDate(dateFormat, v);
  108. ok = ok && (!dates[i] || (dp.datepick('isSelectable', dates[i]) &&
  109. (!minDate || dates[i].getTime() >= minDate.getTime()) &&
  110. (!maxDate || dates[i].getTime() <= maxDate.getTime())));
  111. });
  112. }
  113. catch (e) {
  114. ok = false;
  115. }
  116. }
  117. if (ok && rangeSelect) {
  118. ok = (dates[0].getTime() <= dates[1].getTime());
  119. }
  120. return ok;
  121. }
  122. /* And allow as a class rule. */
  123. $.validator.addClassRules('dpDate', {dpDate: true});
  124. var comparisons = {equal: 'eq', same: 'eq', notEqual: 'ne', notSame: 'ne',
  125. lessThan: 'lt', before: 'lt', greaterThan: 'gt', after: 'gt',
  126. notLessThan: 'ge', notBefore: 'ge', notGreaterThan: 'le', notAfter: 'le'};
  127. /* Cross-validate date fields.
  128. params should be an array with [0] comparison type eq/ne/lt/gt/le/ge or synonyms,
  129. [1] 'today' or date string or Date or other field selector/element/jQuery OR
  130. an object with one attribute with name eq/ne/lt/gt/le/ge or synonyms
  131. and value 'today' or date string or Date or other field selector/element/jQuery OR
  132. a string with eq/ne/lt/gt/le/ge or synonyms followed by 'today' or date string or jQuery selector */
  133. $.validator.addMethod('dpCompareDate', function(value, element, params) {
  134. if (this.optional(element)) {
  135. return true;
  136. }
  137. params = normaliseParams(params);
  138. var thisDate = $(element).datepick('getDate');
  139. var thatDate = extractOtherDate(element, params[1]);
  140. if (thisDate.length == 0 || thatDate.length == 0) {
  141. return true;
  142. }
  143. lastElement = element;
  144. var finalResult = true;
  145. for (var i = 0; i < thisDate.length; i++) {
  146. switch (comparisons[params[0]] || params[0]) {
  147. case 'eq': finalResult = (thisDate[i].getTime() == thatDate[0].getTime()); break;
  148. case 'ne': finalResult = (thisDate[i].getTime() != thatDate[0].getTime()); break;
  149. case 'lt': finalResult = (thisDate[i].getTime() < thatDate[0].getTime()); break;
  150. case 'gt': finalResult = (thisDate[i].getTime() > thatDate[0].getTime()); break;
  151. case 'le': finalResult = (thisDate[i].getTime() <= thatDate[0].getTime()); break;
  152. case 'ge': finalResult = (thisDate[i].getTime() >= thatDate[0].getTime()); break;
  153. default: finalResult = true;
  154. }
  155. if (!finalResult) {
  156. break;
  157. }
  158. }
  159. return finalResult;
  160. },
  161. function(params) {
  162. var messages = $.datepick._defaults;
  163. var inst = $.data(lastElement, $.datepick.dataName);
  164. params = normaliseParams(params);
  165. var thatDate = extractOtherDate(lastElement, params[1], true);
  166. thatDate = (params[1] == 'today' ? messages.validateDateToday : (thatDate.length ?
  167. $.datepick.formatDate(inst.get('dateFormat'), thatDate[0], inst.getConfig()) :
  168. messages.validateDateOther));
  169. return messages.validateDateCompare.replace(/\{0\}/,
  170. messages['validateDate' + (comparisons[params[0]] || params[0]).toUpperCase()]).
  171. replace(/\{1\}/, thatDate);
  172. });
  173. /* Normalise the comparison parameters to an array.
  174. @param params (array or object or string) the original parameters
  175. @return (array) the normalised parameters */
  176. function normaliseParams(params) {
  177. if (typeof params == 'string') {
  178. params = params.split(' ');
  179. }
  180. else if (!$.isArray(params)) {
  181. var opts = [];
  182. for (var name in params) {
  183. opts[0] = name;
  184. opts[1] = params[name];
  185. }
  186. params = opts;
  187. }
  188. return params;
  189. }
  190. /* Determine the comparison date.
  191. @param element (element) the current datepicker element
  192. @param source (string or Date or jQuery or element) the source of the other date
  193. @param noOther (boolean) true to not get the date from another field
  194. @return (Date[1]) the date for comparison */
  195. function extractOtherDate(element, source, noOther) {
  196. if (source.constructor == Date) {
  197. return [source];
  198. }
  199. var inst = $.data(element, $.datepick.dataName);
  200. var thatDate = null;
  201. try {
  202. if (typeof source == 'string' && source != 'today') {
  203. thatDate = $.datepick.parseDate(inst.get('dateFormat'), source, inst.getConfig());
  204. }
  205. }
  206. catch (e) {
  207. // Ignore
  208. }
  209. thatDate = (thatDate ? [thatDate] : (source == 'today' ?
  210. [$.datepick.today()] : (noOther ? [] : $(source).datepick('getDate'))));
  211. return thatDate;
  212. }
  213. }
  214. })(jQuery);