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