You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zhky/web/css/dateFormat.htc

148 lines
2.6 KiB

<PUBLIC:PROPERTY NAME="mask" value="yyyy-mm-dd"/>
<public:attach event="onblur" onevent="itemOnBlur()" />
<public:method name="validate">
<script language="javascript">
function validate() {
if (element.value.length == 0) return true;
if (!dateValidation(element,mask)) {
element.focus();
element.value="";
}
}
function itemOnBlur(){
validate();
}
//1#辅助函数(闰年判定)
function isLeapYear(year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
return true;
}
return false;
}
//2#判定主函数
function dateValidation(dateObj,format)
{
var regexp,value,index;
var year,month,day;
var iyear,imonth,iday;
var fmt,regfmt,ordfmt;
var dateArray;
/*
if(isObject(dateObj))
{
value=dateObj.value;
}
else if(isString(dateobj)&&!isEmpty(dateobj))
{
value=dateobj;
}
else
{
return false;
}
if(isEmpty(format))
{
return false;
}
*/
var value = dateObj.value;
if (value.length==0) {
return false;
}
fmt=new Array("yyyy/mm/dd","mm/dd/yyyy","dd/mm/yyyy","yyyy-mm-dd");
regfmt=new Array("/^([0-9]{4})\\/([0-9]{2})\\/([0-9]{2})$/","/^([0-9]{2})\\/([0-9]{2})\\/([0-9]{4})$/","/^([0-9]{2})\\/([0-9]{2})\\/([0-9]{4})$/","/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/");
ordfmt=new Array("123","312","321","123");
format=format.toLowerCase();
for(index=0;index<fmt.length;index++)
{
if(format==fmt[index])
{
eval('regexp='+regfmt[index]+';');
iyear=parseInt(ordfmt[index].charAt(0));
imonth=parseInt(ordfmt[index].charAt(1));
iday=parseInt(ordfmt[index].charAt(2));
break;
}
}
if(index==fmt.length)
{
alert("不支持该日期格式: "+mask);
return false;
}
if(regexp.test(value)){
dateArray=value.match(regexp);
year=dateArray[iyear];
month=dateArray[imonth];
day=dateArray[iday];
//alert("你输入的日期是: \nYear:"+year+"\nMonth:"+month+"\nDay:"+day);
if(year<1900)
{
alert("年份必须大于1900!");
return false;
}
if(month<1||month>12)
{
alert("月份的范围是1到12!");
return false;
}
if(day<1||day>31)
{
alert("日期的范围是1到31!");
return false;
}
else
{
if(month==2)
{
if(isLeapYear(year)&&day>29)
{
alert("二月份的日期范围是1到29!");
return false;
}
if(!isLeapYear(year)&&day>28)
{
alert("二月份的日期范围是1到28!");
return false;
}
}
if((month==4||month==6||month==9||month==11)&&(day>30))
{
alert("这个月的日期范围是1到30!");
return false;
}
}
}
else
{
alert("日期格式错误!\n正确的日期格式: "+format);
return false;
}
return true;
}
</script>