请教一个判断日期是否合法的javascript  现在要用脚本来判断一个用户输入的日期是否合法。格式为 dd/MM/yyyy 请教js检测的方法。

解决方案 »

  1.   

    给一个判断日期的函数让你参考://函数名:chkdate
    //功能介绍:检查是否为日期
    //参数说明:要检查的字符串
    //返回值:0:不是日期  1:是日期
    function chkdate(datestr)
    {
     var lthdatestr
     if (datestr != "")
      lthdatestr= datestr.length ;
     else
      lthdatestr=0;
      
     var tmpy="";
     var tmpm="";
     var tmpd="";
     //var datestr;
     var status;
     status=0;
     if ( lthdatestr== 0)
      return 0 
     for (i=0;i<lthdatestr;i++)
     { if (datestr.charAt(i)== '/')
      {
       status++;
      }
      if (status>2)
      {
       //alert("Invalid format of date!");
       return 0;
      }
      if ((status==0) && (datestr.charAt(i)!='/'))
      {
       tmpy=tmpy+datestr.charAt(i)
      }
      if ((status==1) && (datestr.charAt(i)!='/'))
      {
       tmpm=tmpm+datestr.charAt(i)
      }
      if ((status==2) && (datestr.charAt(i)!='/'))
      {
       tmpd=tmpd+datestr.charAt(i)
      } 
     }
     year=new String (tmpy);
     month=new String (tmpm);
     day=new String (tmpd)
     //tempdate= new String (year+month+day);
     //alert(tempdate);
     if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2))
     {
      //alert("Invalid format of date!");
      return 0;
     }
     if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
     {
      //alert ("Invalid month or day!");
      return 0;
     }
     if (!((year % 4)==0) && (month==2) && (day==29))
     {
      //alert ("This is not a leap year!");
      return 0;
     }
     if ((month<=7) && ((month % 2)==0) && (day>=31))
     {
      //alert ("This month is a small month!");
      return 0;
     }
     if ((month>=8) && ((month % 2)==1) && (day>=31))
     {
      //alert ("This month is a small month!");
      return 0;
     }
     if ((month==2) && (day==30))
     {
      //alert("The Febryary never has this day!");
      return 0;
     }
     return 1;
    }
      

  2.   

    function IsValidYear(psYear)
    {
        var sYear = new String(psYear);
        if(psYear==null)
        {
            return false;
        }
        if(isNaN(psYear)==true)
        {
            return false;
        }    if(sYear == "")
        {
            return true;
        }    if(sYear.match(/[^0-9]/g)!=null)
        {
            return false;
        }    var nYear = parseInt(sYear, 10);    if((nYear < 0) (9999 < nYear))
        {
            return false;
        }    return true;
    }
    function IsValidMonth(psMonth)
    {
        var sMonth = new String(psMonth);    if(psMonth==null)
        {
            return false;
        }    if(isNaN(psMonth)==true)
        {
            return false;
        }    if(sMonth == "")
        {
            return true;
        }    if(sMonth.match(/[^0-9]/g)!=null)
        {
            return false;
        }    var nMonth = parseInt(sMonth,10);    if((nMonth < 0) (12 < nMonth))
        {
            return false;
        }    return true;
    }
    function IsValidDay(psDay)
    {
        var sDay  = new String(psDay);    if(psDay==null)
        {
            return false;
        }    if(isNaN(psDay)==true)
        {
            return false;
        }    if(sDay == "")
        {
            return true;
        }    if(sDay.match(/[^0-9]/g)!=null)
        {
            return false;
        }    var nDay = parseInt(psDay, 10);    if((nDay < 0) (31 < nDay))
        {
            return false;
        }    return true;
    }
    function IsValidDate(psYear, psMonth, psDay)
    {
        if(psYear==null psMonth==null psDay==null)
        {
            return false;
        }    var sYear  = new String(psYear);
        var sMonth = new String(psMonth);
        var sDay   = new String(psDay);    if(IsValidYear(sYear)==false)
        {
            return false;
        }    if(IsValidMonth(sMonth)==false)
        {
            return false;
        }    if(IsValidDay(sDay)==false)
        {
            return false;
        }    var nYear  = parseInt(sYear,  10);
        var nMonth = parseInt(sMonth, 10);
        var nDay   = parseInt(sDay,   10);    if(sYear=="" &&  sMonth=="" && sDay=="")
        {
            return true;
        }    if(sYear=="" sMonth=="" sDay=="")
        {
            return false;
        }
        
        if(nMonth < 1 12 < nMonth)
        {
            return false;
        }
        if(nDay < 1 31 < nDay)
        {
            return false;
        }    if(nMonth == 2)
        {
            if((nYear % 400 == 0) (nYear % 4 == 0) && (nYear % 100 != 0))
            {
                if((nDay < 1) (nDay > 29))
                {
                    return false;
                }
            }
            else 
            {
                if((nDay < 1) (nDay > 28))
                {
                    return false;
                }
            }
        }
        else if((nMonth == 1)  
                (nMonth == 3)  
                (nMonth == 5)  
                (nMonth == 7)  
                (nMonth == 8)  
                (nMonth == 10) 
                (nMonth == 12))
        {
            if((nDay < 1) (31 < nDay))
            {
                return false;
            }
        }
        else 
        {
            if((nDay < 1) (30 < nDay))
            {
                return false;
            }
        }    return true;
    }
      

  3.   

      function CheckDate(obj)   
      {   
      var strDate,arrDate;
      var lngYear,lngMonth,lngDay;
      var strReg; 
      var strError;     
      strError = "" ;  
      strReg = /^\d{4}-\d{2}-\d{2}$/;
      strDate = obj.value;
      arrDate = strDate.split("-");
      if (strReg.test(strDate))   
      {   
      lngYear = parseInt(arrDate[0],10); 
      lngMonth = parseInt(arrDate[1],10);  
      lngDay = parseInt(arrDate[2], 10);    
      // alert(lngYear   +   ","   +   lngMonth   +   ","   +   lngDay)   
      // return   
      }       
      if (!strReg.test(strDate))
      {   
    strError = "格式错误!\n正确格式: yyyy-mm-dd";
      }   
      else if(lngMonth<1||lngMonth>12)
      {   
    strError = "月份应在01-12之间!"   
      }   
      else if (lngDay<1||lngDay>GetDay(lngYear,lngMonth))
      {   
    strError = "天数应在01-"+ GetDay(lngYear,lngMonth)+"之间!";   
      }       
      if (strError != "")
      {   
     alert(strError);   
     obj.select() ; 
     obj.focus();  
      }   
      }