1. 程式人生 > >EasyUI 時間格式驗證

EasyUI 時間格式驗證

$.extend($.fn.validatebox.defaults.rules, {
	dateCompareLE:{//小於等於
   	 	validator: function(value, param){ 
	   	 	var varify = /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29))$/i.test(value);
	   	 	if(varify){
		   	 	var endTime = $(param[0]).datetimebox('getValue');
		   	 	if(endTime==""){
		   	 		return true;
		   	 	}
		   	 	//if()
		  		var dateEnd = $.fn.datebox.defaults.parser(endTime); 
		  		var dateStart = $.fn.datebox.defaults.parser(value);
		  		varify = dateStart <= dateEnd; 
		  		if(!varify){
		  			$.fn.validatebox.defaults.rules.dateCompareLE.message ="起始時間要小於或等於截止時間";
		  		}
		  		return varify;
	   	 	}else{
	   	 		$.fn.validatebox.defaults.rules.dateCompareLE.message ="請輸入正確的日期格式(yyyy-mm-dd)";
	   	 		return false;
	   	 	}
   	 	},
   	 	message: ''
   	},
	dateCompareGE:{//大於等於
   	 	validator: function(value, param){ 
	   	 	var varify = /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29))$/i.test(value);
	   	 	if(varify){
		   	 	var startTime = $(param[0]).datetimebox('getValue');
		   	 	if(startTime==""){
		   	 		return true;
		   	 	}
		  		var dateStart = $.fn.datebox.defaults.parser(startTime); 
		  		var dateEnd = $.fn.datebox.defaults.parser(value);
		  		
		  		varify = dateEnd >= dateStart; 
		  		if(!varify){
		  			$.fn.validatebox.defaults.rules.dateCompareGE.message ="截止時間要大於或等於起始時間";
		  		}
		  		return varify;
	   	 	}else{
	   	 		$.fn.validatebox.defaults.rules.dateCompareGE.message ="請輸入正確的日期格式(yyyy-mm-dd)";
	   	 		return false;
	   	 	}
   	 	}
   	},
   	dateTimeCompareLE:{//小於等於
   	 	validator: function(value, param){ 
	   	 	var varify = /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29)) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/i.test(value);
	   	 	if(varify){
		   	 	var endTime = $(param[0]).datetimebox('getValue');
		   	 	if(endTime==""){
		   	 		return true;
		   	 	}
		   	 	var dateEnd = new Date(Date.parse(endTime.replace(/-/g,"/"))).getTime(); 
		   	 	var dateStart = new Date(Date.parse(value.replace(/-/g,"/"))).getTime();
		  		varify = dateStart <= dateEnd; 
		  		if(!varify){
		  			$.fn.validatebox.defaults.rules.dateTimeCompareLE.message ="起始時間要小於或等於截止時間";
		  		}
		  		return varify;
	   	 	}else{
	   	 		$.fn.validatebox.defaults.rules.dateTimeCompareLE.message ="請輸入正確的日期格式(yyyy-mm-dd hh:mm:ss)";
	   	 		return false;
	   	 	}
   	 	},
   	 	message: ''
   	},
   	dateTimeCompareGE:{//大於等於
   	 	validator: function(value, param){ 
	   	 	var varify = /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29)) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/i.test(value);
	   	 	if(varify){
		   	 	var startTime = $(param[0]).datetimebox('getValue');
		   	 	if(startTime==""){
		   	 		return true;
		   	 	}
		  		var dateEnd = new Date(Date.parse(value.replace(/-/g,"/"))).getTime(); 
		   	 	var dateStart = new Date(Date.parse(startTime.replace(/-/g,"/"))).getTime();
		  		varify = dateEnd >= dateStart; 
		  		if(!varify){
		  			$.fn.validatebox.defaults.rules.dateTimeCompareGE.message ="截止時間要大於或等於起始時間";
		  		}
		  		return varify;
	   	 	}else{
	   	 		$.fn.validatebox.defaults.rules.dateTimeCompareGE.message ="請輸入正確的日期格式(yyyy-mm-dd hh:mm:ss)";
	   	 		return false;
	   	 	}
   	 	}
   	},
});