12345678 |
- /* http://keith-wood.name/datepick.html
- Datepicker Validation extension for jQuery 4.0.6.
- Requires Jörn Zaefferer's Validation plugin (http://plugins.jquery.com/project/validate).
- Written by Keith Wood (kbwood{at}iinet.com.au).
- Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
- MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
- Please attribute the author if you use it. */
- (function($){if($.fn.validate){$.datepick.selectDateOrig=$.datepick.selectDate;$.extend($.datepick.regional[''],{validateDate:'Please enter a valid date',validateDateMin:'Please enter a date on or after {0}',validateDateMax:'Please enter a date on or before {0}',validateDateMinMax:'Please enter a date between {0} and {1}',validateDateCompare:'Please enter a date {0} {1}',validateDateToday:'today',validateDateOther:'the other date',validateDateEQ:'equal to',validateDateNE:'not equal to',validateDateLT:'before',validateDateGT:'after',validateDateLE:'not after',validateDateGE:'not before'});$.extend($.datepick._defaults,$.datepick.regional['']);$.extend($.datepick,{selectDate:function(a,b){this.selectDateOrig(a,b);var c=$.data(a,$.datepick.dataName);if(!c.inline&&$.fn.validate){var d=$(a).parents('form').validate();if(d){d.element('#'+a.id)}}},errorPlacement:function(a,b){var c=$.data(b[0],$.datepick.dataName);if(c){a[c.get('isRTL')?'insertBefore':'insertAfter'](c.trigger.length>0?c.trigger:b)}else{a.insertAfter(b)}},errorFormat:function(c,d){var e=($.datepick.curInst?$.datepick.curInst.get('dateFormat'):$.datepick._defaults.dateFormat);$.each(d,function(a,b){c=c.replace(new RegExp('\\{'+a+'\\}','g'),$.datepick.formatDate(e,b)||'nothing')});return c}});var n=null;$.validator.addMethod('dpDate',function(a,b){n=b;return this.optional(b)||validateEach(a,b)},function(a){var b=$.data(n,$.datepick.dataName);var c=b.get('minDate');var d=b.get('maxDate');var e=$.datepick._defaults;return(c&&d?$.datepick.errorFormat(e.validateDateMinMax,[c,d]):(c?$.datepick.errorFormat(e.validateDateMin,[c]):(d?$.datepick.errorFormat(e.validateDateMax,[d]):e.validateDate)))});function validateEach(a,b){var c=$.data(b,$.datepick.dataName);var d=c.get('rangeSelect');var f=c.get('multiSelect');var g=(f?a.split(c.get('multiSeparator')):(d?a.split(c.get('rangeSeparator')):[a]));var h=(f&&g.length<=f)||(!f&&d&&g.length==2)||(!f&&!d&&g.length==1);if(h){try{var j=c.get('dateFormat');var k=c.get('minDate');var l=c.get('maxDate');var m=$(b);$.each(g,function(i,v){g[i]=$.datepick.parseDate(j,v);h=h&&(!g[i]||(m.datepick('isSelectable',g[i])&&(!k||g[i].getTime()>=k.getTime())&&(!l||g[i].getTime()<=l.getTime())))})}catch(e){h=false}}if(h&&d){h=(g[0].getTime()<=g[1].getTime())}return h}$.validator.addClassRules('dpDate',{dpDate:true});var o={equal:'eq',same:'eq',notEqual:'ne',notSame:'ne',lessThan:'lt',before:'lt',greaterThan:'gt',after:'gt',notLessThan:'ge',notBefore:'ge',notGreaterThan:'le',notAfter:'le'};$.validator.addMethod('dpCompareDate',function(a,b,c){if(this.optional(b)){return true}c=normaliseParams(c);var d=$(b).datepick('getDate');var e=extractOtherDate(b,c[1]);if(d.length==0||e.length==0){return true}n=b;var f=true;for(var i=0;i<d.length;i++){switch(o[c[0]]||c[0]){case'eq':f=(d[i].getTime()==e[0].getTime());break;case'ne':f=(d[i].getTime()!=e[0].getTime());break;case'lt':f=(d[i].getTime()<e[0].getTime());break;case'gt':f=(d[i].getTime()>e[0].getTime());break;case'le':f=(d[i].getTime()<=e[0].getTime());break;case'ge':f=(d[i].getTime()>=e[0].getTime());break;default:f=true}if(!f){break}}return f},function(a){var b=$.datepick._defaults;var c=$.data(n,$.datepick.dataName);a=normaliseParams(a);var d=extractOtherDate(n,a[1],true);d=(a[1]=='today'?b.validateDateToday:(d.length?$.datepick.formatDate(c.get('dateFormat'),d[0],c.getConfig()):b.validateDateOther));return b.validateDateCompare.replace(/\{0\}/,b['validateDate'+(o[a[0]]||a[0]).toUpperCase()]).replace(/\{1\}/,d)});function normaliseParams(a){if(typeof a=='string'){a=a.split(' ')}else if(!$.isArray(a)){var b=[];for(var c in a){b[0]=c;b[1]=a[c]}a=b}return a}function extractOtherDate(a,b,c){if(b.constructor==Date){return[b]}var d=$.data(a,$.datepick.dataName);var f=null;try{if(typeof b=='string'&&b!='today'){f=$.datepick.parseDate(d.get('dateFormat'),b,d.getConfig())}}catch(e){}f=(f?[f]:(b=='today'?[$.datepick.today()]:(c?[]:$(b).datepick('getDate'))));return f}}})(jQuery);
|