function DateValid(objName) 
{
   var strDate;
   var strDateArray;
   var strDay;
   var strMonth;
   var strYear;
   var intday;
   var intMonth;
   var intYear;
   var booFound = false;
   var datefield = objName;
   var strSeparatorArray = new Array("-"," ","/",".");
   var intElementNr;
   // var err = 0;
   var strMonthArray = new Array(12);
   strMonthArray[0] = "Jan";
   strMonthArray[1] = "Feb";
   strMonthArray[2] = "Mar";
   strMonthArray[3] = "Apr";
   strMonthArray[4] = "May";
   strMonthArray[5] = "Jun";
   strMonthArray[6] = "Jul";
   strMonthArray[7] = "Aug";
   strMonthArray[8] = "Sep";
   strMonthArray[9] = "Oct";
   strMonthArray[10] = "Nov";
   strMonthArray[11] = "Dec";
      
   //strDate = datefield.value;
   objName.value = convWidToHalf(objName.value);  // Added by Neusoft
   strDate = objName.value;    
      
   for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
   {
      if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
      {
         strDateArray = strDate.split(strSeparatorArray[intElementNr]);
         if (strDateArray.length != 3) 
         {
            return false;
         }
         else 
         {
            strYear = strDateArray[0];
            strMonth = strDateArray[1];
            strDay = strDateArray[2];         }
         booFound = true;
      }
   }
   if (booFound == false) 
   {
       return false; // Add by Neusoft,2004/03/10
      if (strDate.length>5) 
      {
         strDay = strDate.substr(0, 2);
         strMonth = strDate.substr(2, 2);
         strYear = strDate.substr(4);
      }
   }
   //Adjustment for short years entered
//   if (strYear.length == 2) 
//   {
//      strYear = '20' + strYear;
//   }
//   if (strDay.length != 2) 
//   {
//      return false;
//   }
//   if (strMonth.length != 2) 
//   {
//      return false;
//   }
   if (strYear.length != 4) 
   {
      return false;
   }   intday = parseInt(strDay, 10);
   if (isNaN(intday)) 
   {
      return false;
   }
      
   intMonth = parseInt(strMonth, 10);
   if (isNaN(intMonth)) 
   {
      for (i = 0;i<12;i++) 
      {
         if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
         {
            intMonth = i+1;
            strMonth = strMonthArray[i];
            i = 12;
         }
      }
      if (isNaN(intMonth)) 
      {
         return false;
      }
   }   intYear = parseInt(strYear, 10);
   if (isNaN(intYear)) 
   {
      return false;
   }
   if (intMonth>12 || intMonth<1) 
   {
      return false;
   }
   if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
   {
      return false;
   }
   if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
   {
      return false;
   }
   if (intMonth == 2) 
   {
      if (intday < 1) 
      {
         return false;
      }
      if (!LeapYear(intYear)) 
      {
         if (intday > 29) 
         {
            return false;
         }
      }
      else
      {
         if (intday > 28) 
         {
            return false;
         }
      }
   }
   return true;
   
}

解决方案 »

  1.   

    //日期格式:YYYY-MM-DD
    function isdate(strDate){
       var strSeparator = "-"; //日期分隔符
       var strDateArray;
       var intYear;
       var intMonth;
       var intDay;
       var boolLeapYear;
       
       strDateArray = strDate.split(strSeparator);
       
       if(strDateArray.length!=3) return false;
       
       intYear = parseInt(strDateArray[0],10);
       intMonth = parseInt(strDateArray[1],10);
       intDay = parseInt(strDateArray[2],10);
       
       if(isNaN(intYear)||isNaN(intMonth)||isNaN(intDay)) return false;
       
       if(intMonth>12||intMonth<1) return false;
       
       if((intMonth==1||intMonth==3||intMonth==5||intMonth==7||intMonth==8||intMonth==10||intMonth==12)&&(intDay>31||intDay<1)) return false;
       
       if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)&&(intDay>30||intDay<1)) return false;
       
       if(intMonth==2){
          if(intDay<1) return false;
          
          boolLeapYear = false;
          if((intYear%100)==0){
             if((intYear%400)==0) boolLeapYear = true;
          }
          else{
             if((intYear%4)==0) boolLeapYear = true;
          }
          
          if(boolLeapYear){
             if(intDay>29) return false;
          }
          else{
             if(intDay>28) return false;
          }
       }
       
       return true;
    }