判断输入的日期是否合法的javascript

来源:互联网 发布:angularjs 源码 编辑:程序博客网 时间:2024/06/07 20:09

判断输入的日期是否合法的javascript:

function   isDate()
        {  
    var   datetime;  
    var   year,month,day;      
    var start = document.getElementById("ttt:start").value;          
      if(start.length==8)
      { 
         year=start.substring(0,4);
         month=start.substring(4,6);
         day=start.substring(6,8); 
        if(isNaN(year)==true ||isNaN(month)==true ||isNaN(day)==true)
         {  
           alert("请输入日期格式为(yyyyMMDD)   /n例(20010101)!");  
          
           return   false;  
         }                
       
         if(isLeapYear(year))
         {
           if(month==2)
           {
                if(day>29)
                {
                  alert("闰年二月的天数最多29天");
              return   false;
             }
            }
         }
         if(!isLeapYear(year))
         {
           if(month==2)
           {
                if(day>28)
                {
                  alert("非闰年二月的天数最多28天");
              return   false;
             }
            }
         }
         if(month>12||month==0)
        {
         alert("月份为1~12")
        }
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        {
         if(day>31||day==0)
         {
          alert("一、三、五、七、八、十、十二月的天数为1~31!");
         }
        }
        if(month==4||month==6||month==9||month==11)
        {
         if(day>30||day==0)
         {
          alert("四、六、九、十一月的天数为1~30!");
         }
        }       
      }
      else
      {
       alert("请输入日期格式为(yyyyMMDD)   /n例(20010101)!");
      }     
  
        }