给你个参考:function sTrim(str){
        var i,j,s;
        if(str == null){
           return '';
        }
        if(str.length == 0){
           return '';
        }
        for(i = 0;i < str.length;i ++){
          if(str.charAt(i) != ' '){
             break;
          }
        }
        if(i >= str.length){
           return '';
        }
        for(j = str.length - 1;j >= 0;j --){
           if(str.charAt(j) != ' '){
              break;
           }
        }
        s = str.substring(i,j + 1);
        return s;
    }
function IsDate(str1,str2,str3){
         if(parseInt(sTrim(str1))){
            if(sTrim(str1).length==4){
                var yy=parseInt(str1);
                var isY=0;
                if(((yy%100!=0)&&(yy%4==0))||(yy%400==0)){
                   isY=1;
                }else{
                   isY=0;
                }
                if(isY==1){
                     switch(parseInt(str2)){
                          case 2:
                              if(parseInt(str3)>29){
                                  alert("二月没这一天。");
                                  return false;
                              }
                          case 4:
                              if(parseInt(str3)>30){
                                  alert("四月没这一天。");
                                  return false;
                              }    
                          case 6:
                              if(parseInt(str3)>30){
                                  alert("六月没这一天。");
                                  return false;
                              } 
                          case 9:
                              if(parseInt(str3)>30){
                                  alert("九月没这一天。");
                                  return false;
                              }    
                          case 11:
                              if(parseInt(str3)>30){
                                  alert("十一月没这一天。");
                                  return false;
                              }       
                     };
               }else{
                     switch(parseInt(str2)){
                          case 2:
                              if(parseInt(str3)>28){
                                  alert("二月没这一天。");
                                  return false;
                              }
                          case 4:
                              if(parseInt(str3)>30){
                                  alert("四月没这一天。");
                                  return false;
                              }    
                          case 6:
                              if(parseInt(str3)>30){
                                  alert("六月没这一天。");
                                  return false;
                              } 
                          case 9:
                              if(parseInt(str3)>30){
                                  alert("九月没这一天。");
                                  return false;
                              }    
                          case 11:
                              if(parseInt(str3)>30){
                                  alert("十一月没这一天。");
                                  return false;
                              }       
                     };                 
               }
                         return true;
            }else{
                alert("年份为四位数(如:2001)。");
                return false;
            } 
       }else{
                alert("请输入正确的年份。");
                return false;
       }
    }
function dateKeyEvent(edit) {
    var sDate = sTrim(edit.value);
    var m_day = new Array();
    m_day[0] = 31;
    m_day[1] = 28;
    m_day[2] = 31;
    m_day[3] = 30;
    m_day[4] = 31;
    m_day[5] = 30;
    m_day[6] = 31;
    m_day[7] = 31;
    m_day[8] = 30;
    m_day[9] = 31;
    m_day[10] = 30;
    m_day[11] = 31;
    var ch = sDate.charAt(sDate.length - 1);
    if((sDate.length < 5) || ((sDate.length > 5) && (sDate.length < 8)) || ((sDate.length > 8) && (sDate.length < 11))){
       if((ch < '0') || (ch > '9')){
          edit.value = sDate.substring(0,sDate.length - 1);
          return;
       }
    }
    if((sDate.length == 4) || (sDate.length == 7)){
       sDate = sDate+"-";
       edit.value = sDate;
    }
    if(sDate.length == 10){
       var year = parseInt(sDate.substring(0,4),10);
       var month = parseInt(sDate.substring(5,7),10);
       if((month > 12) || (month < 1)){
          sDate = sDate.substring(0,5);
          edit.value = "'"+month+"'";
          return;
       } else {
          var day = parseInt(sDate.substring(8),10);
          if(month == 2){
             if((year % 400) == 0){
                if((day < 1) || (day > (m_day[month - 1] + 1))){
                   sDate = sDate.substring(0,8);
                   edit.value = sDate;
                   return;
                }
             } else {
                if(((year % 4) == 0) && ((year % 100) != 0)){
                   if((day < 1) || (day > (m_day[month - 1] + 1))){
                      sDate = sDate.substring(0,8);
                      edit.value = sDate;
                      return;
                   }
                } else {
                   if((day < 1) || (day > (m_day[month - 1]))){
                      sDate = sDate.substring(0,8);
                      edit.value = sDate;
                      return;
                   }
                }
             }
          } else {
             if((day < 1) || (day > m_day[month - 1])){
                sDate = sDate.substring(0,8);
                edit.value = sDate;
                return;
             }
          }
       }
    } else {
       if(sDate.length > 10){
          sDate = sDate.substring(0,10);
          edit.value = sDate;
       }
    }
}function dateBlurEvent(edit) {
    var sDate = sTrim(edit.value);
    var m_day = new Array();
    m_day[0] = 31;
    m_day[1] = 28;
    m_day[2] = 31;
    m_day[3] = 30;
    m_day[4] = 31;
    m_day[5] = 30;
    m_day[6] = 31;
    m_day[7] = 31;
    m_day[8] = 30;
    m_day[9] = 31;
    m_day[10] = 30;
    m_day[11] = 31;
    var ch;
    if(sDate.length != 10){
       edit.value = "";
       return;
    }
    for(i = 0;i < 10;i ++){
       ch = sDate.charAt(i);
       if(((i >= 0) && (i <= 3)) || ((i >= 5) && (i <= 6)) || ((i >= 8) && (i <= 9))){
          if((ch < '0') || (ch > '9')){
             edit.value = "";
             return;
          }
       } else {
          if((i == 4) || (i == 7)){
             if(ch != '-'){
             edit.value = "";
             return;
             }
          }
       }
    }
    var year = parseInt(sDate.substring(0,4),10);
    var month = parseInt(sDate.substring(5,7),10);
    var day = parseInt(sDate.substring(8),10);
    if((month > 12) || (month < 1)){
       edit.value = "";
       return;
    }
    if(month == 2){
       if((year % 400) == 0){
          if((day < 1) || (day > (m_day[month - 1] + 1))){
             edit.value = "";
             return;
          }
       } else {
          if(((year % 4) == 0) && ((year % 100) != 0)){
             if((day < 1) || (day > (m_day[month - 1] + 1))){
                edit.value = "";
                return;
             }
          } else {
             if((day < 1) || (day > (m_day[month - 1]))){
                edit.value = "";
                return;
             }
          }
       }
    } else {
       if((day < 1) || (day > m_day[month - 1])){
          edit.value = "";
          return;
       }
    }
 }

解决方案 »

  1.   

    这个用正则比较好些!!
    "if(event.srcElement.value!=''){" & _
                            "var zz=/^(\d{4})[-, ,|,\/,\\,\.,\,](\d{1,2})[-, ,|,\/,\\,\.\,](\d{1,2})$/;" & _
                            "if(zz.test(event.srcElement.value)){" & _
                                "var kk=/^(\d{2})$/;" & _
                                "var strYear=event.srcElement.value.substring(0,4);" & _
                                "var strMonth=event.srcElement.value.substring(5,7);" & _
                                "var strDay=event.srcElement.value.substring(event.srcElement.value.length-2,event.srcElement.value.length);" & _
                                "if(!kk.test(strMonth)){" & _
                                    "strMonth=event.srcElement.value.substring(5,6);}" & _
                                "if(!kk.test(strDay)){" & _
                                    "strDay=event.srcElement.value.substring(event.srcElement.value.length-1,event.srcElement.value.length);}" & _
                                "if(strYear<1900||strYear>2099){" & _
                                    "alert('输入日期的年份超出范围!');" & _
                                    "event.srcElement.focus();}" & _
                                "else{" & _
                                    "if(strMonth<1||strMonth>12){" & _
                                        "alert('输入日期的月份超出范围!');" & _
                                        "event.srcElement.focus();}" & _
                                    "else{" & _
                                        "var strLastDay;" & _
                                        "switch(strMonth){" & _
                                            "case '2':if(strYear%4==0){strLastDay=28;break;}else{strLastDay=29;break;}" & _
                                            "case '4':{strLastDay=30;break;}" & _
                                            "case '6':{strLastDay=30;break;}" & _
                                            "case '9':{strLastDay=30;break;}" & _
                                            "case '11':{strLastDay=30;break;}" & _
                                            "default:{strLastDay=31;break;}}" & _
                                        "if(strDay>strLastDay||strDay==0){" & _
                                            "alert('输入日期超出范围!');" & _
                                            "event.srcElement.focus();}" & _
                                        "else{" & _
                                            "var strDate=new Date(strYear,strMonth-1,strDay);" & _
                                            "if(strDate=='NaN'){" & _
                                                "alert('输入日期的逻辑有误!');" & _
                                                "event.srcElement.focus();}" & _
                                            "else{" & _
                                                "event.srcElement.className='textbox';}" & _
                                        "}" & _
                                    "}" & _
                                "}" & _
                            "}" & _
                            "else{" & _
                                "alert('请输入正确的日期类型!(YYYY-MM-DD)');" & _
                                "event.srcElement.focus();}" & _
                        "}" & _
                        "else{" & _
                            "event.srcElement.className='textbox';}"            Output.AddAttribute("onblur", strJS)通过这个你自已想想就OK了!!
      

  2.   

    function checkDate (date)
    {
    var strValue  = new String();
    strValue = date;
    var year = new String();
    var month = new String();
    var day = new String();

    switch(strValue.length)
    {
    case 4:
    {
    year = strValue;
    var testDate = new Date(year,1,1);
    return  (year == testDate.getFullYear());
        break;
    }
    case 7:
       {
        if (strValue.charAt(4)!='-')
    {
    return false;
    }
    else

    strValue = value;
    year = strValue.substr(0,4);
    month = strValue.substr(5,2);
    month = month-1; 
    var testDate = new Date(year,month,1)
    return  ((year == testDate.getFullYear()) && (month ==testDate.getMonth()));
    }
    break;

    }

    case 10:
    {
    if (strValue.charAt(4)!='-'||strValue.charAt(7)!='-')
    {
    return false;
    }
    else

    year = strValue.substr(0,4);
    month = strValue.substr(5,2);
    month = month-1; 
    day = strValue.substr(8,2);
    var testDate = new Date(year,month,day)
    return  ((year == testDate.getFullYear()) && (month ==testDate.getMonth())&&(day == testDate.getDate()));
    }   

    break;
    }

    default:
    return false;

    }

    }
      

  3.   

    function checkDate( date ) {
    if ( date == null || date == "" ) {
    return  true;
    } var strDate = date;

    if (NumberFormat( strDate ))
    { if (strDate.length != 10) {
    alert( "错误的日期!" );
    return false ;
    } if ( toDateFromString( strDate ) == null ) {
    alert("错误的日期!");
    return false;
    }
    } return true ;
    }function NumberFormat(pstrNum)
    {
    var strReturn = ""; strNumber = ""+delMoji(pstrNum,"-"); for(var i=0; i<strNumber.length; i++)
    {
    if (strNumber.charAt(i) >= "0" && strNumber.charAt(i) <= "9")
    { }
    else if (strNumber.charAt(i) == "+" || strNumber.charAt(i) == "-" || strNumber.charAt(i) == ".")
    {
    alert("错误的日期!");
    return false;
    }
    } return true;
    }function delMoji(pstrMoji, pstrChar)
    {
    var strReturn ="";
    for(var i=0; i<pstrMoji.length; i++)
    {
    if (pstrMoji.charAt(i) != pstrChar)
    {
    strReturn = strReturn + pstrMoji.charAt(i);
    }
    }
    return strReturn;
    }function toDateFromString( strDate )
    {
    var dtDate = null ;
    var nYear = parseInt( strDate.substring( 0, 4 ), 10 ) ;
    var nMonth = parseInt( strDate.substring( 5, 7 ), 10 ) ;
    var nDay = parseInt( strDate.substring( 8, 10 ), 10 ) ;

    if( isNaN( nYear ) == true || isNaN( nMonth ) == true || isNaN( nDay ) == true )
    {
    return null ;
    }
    dtDate = new Date( nYear, nMonth - 1, nDay ) ;
    if( nYear != dtDate.getFullYear() || ( nMonth - 1 ) != dtDate.getMonth() || nDay != dtDate.getDate() )
    {
    return null ;
    } return dtDate ;
    }
      

  4.   


    function chkDate(strValue)
    {
    strValue=new String(strValue);
         var Reg;
         var iDotPos,iYear,strRight,iMonth,iDay;
         Reg=/^(\d{2}|\d{4})\/(\d{1}|\d{2})\/(\d{1}|\d{2})$/;
         if(Reg.test(strValue))
         {
          iDotPos=strValue.search("\/");
          iYear=strValue.substring(0,iDotPos);
            strRight=strValue.substring(iDotPos+1,strValue.length);
            iDotPos=strRight.search("\/");
            iMonth=strRight.substring(0,iDotPos);
            iDay=strRight.substring(iDotPos+1,strRight.length);
            if(iMonth>12||iMonth==0)
            {
             return false;
            }
            if(iMonth==2)
            {
                if (iYear<100) iYear+=2000;
                if ((iYear % 4 == 0 && iYear % 100 != 0) || iYear % 400 == 0)
                {
                 if(iDay>29||iDay==0) return false;
                }
                else
                {
                 if(iDay>28||iDay==0) return false;
                }
                return true;
            }
            else if(iMonth==1 || iMonth==3 || iMonth == 5 ||iMonth==7 || iMonth==8 || iMonth==10 || iMonth==12)
            {
             if(iDay>31||iDay==0)
             {                 return false;
                    }
            }
            else
            {
             if(iDay>30||iDay==0)
             {            return false;
             }
            }       return true;
         }