function fnCheckData(strValue)
{    
    //strValue 被检测的日期字符
     strValue=new String(strValue);
     var Reg;
     var iDotPos,iYear,strRight,iMonth,iDay;
     //日期的正则表达式,d(n)表示必须有n个数字,"|"表示或的关系,“\/"是转意符表示字符"/",
     //以下字符的意思是符合2002/7/30 ,02/07/3.....等形式的字符表达式
     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;  
     }
     else
     {
     
      return false;
     }
}

解决方案 »

  1.   

    <html> 
    <head> 
    <script Language="JavaScript"> 
    <!-- 
    function testKey(e){ 
    chars= "0123456789/"; 
    e = window.event; 
    if(chars.indexOf(String.fromCharCode(e.keyCode))==-1) window.event.keyCode=0; 
    }; 
    function valDate(M, D, Y){ 
    Months= new Array(31,28,31,30,31,30,31,31,30,31,30,31); 
    Leap = false; if((Y % 4 == 0) && ((Y % 100 != 0) || (Y %400 == 0))) 
    Leap = true; 
    if((D < 1) || (D > 31) || (M < 1) || (M > 12) || (Y < 0)) 
    return(false); 
    if((D > Months[M-1]) && !((M == 2) && (D > 28))) 
    return(false); 
    if(!(Leap) && (M == 2) && (D > 28)) 
    return(false); 
    if((Leap) && (M == 2) && (D > 29)) 
    return(false); 
    }; function formatDate(dateForm){ 
    cDate = dateForm.value; 
    dSize = cDate.length; 
    sCount= 0; if (document.Form1.Date.value == ""){ 
    alert("请输入日期!"); 
    return false ; 
    } if(cDate=='') return; for(var i=0; i < dSize; i++) 
    (cDate.substr(i,1) == "/") ? sCount++ : sCount; 
    if (sCount != 2){ 
    alert("输入的日期格式必须是\n ''月/日/年''"); 
    dateForm.select(); 
    return(false); 
    }; 
    //检测输入的年份是2位数还是4位数; 
    ySize = cDate.substring(cDate.lastIndexOf("/")+1,dSize).length 
    if(ySize<2 || ySize>4 || ySize == 3){ 
    alert('您输入的日期错误 !'); 
    dateForm.select(); 
    return false; 
    }; 
    //将输入的日期字符串分隔成3部分 (Month, Day & Year) 
    idxBarI = cDate.indexOf("/"); 
    idxBarII= cDate.lastIndexOf("/"); 
    strM = cDate.substring(0,idxBarI); 
    strD = cDate.substring(idxBarI+1,idxBarII); 
    strY = cDate.substring(idxBarII+1,dSize); strM = (strM.length < 2 ? '0'+strM : strM); 
    strD = (strD.length < 2 ? '0'+strD : strD); 
    if(strY.length == 2) 
    strY = (strY > 50 ? '19'+strY : '20'+strY); 
    dateForm.value = strM+'/'+strD+'/'+strY; ok = valDate(strM, strD, strY); 
    if(ok==false){ 
    alert("您输入的日期错误 !"); 
    dateForm.select(); 
    return false; 
    }; 
    }; 
    --> 
    </script> <title>日期合法性检测</title> 
    </head> 
    <body onLoad="javascript:document.Form1.Date.focus()" bgcolor="#FFFFFF"> 
    <form name="Form1" method="post" onSubmit="return testKey(event)" action=""> 
    输入正确的日期(月/日/年): 
    <input type=text maxlength =10 name="Date" size=10 onBlur="formatDate(this)" value=""> </form> 
    </body> 
    </html> 说明:此脚本的用途是比较全面地检测输入日期的合法性,除了做非空检测外,还有效地检测了不同年月日期的合法性问题。比如在不是闰年的2月输入了29日等。黄色代码与脚本的检测无关,作用是页面读出页面后光标停留在日期文本框内。可以不要。 注意:(1)<Form>标签中表单的名字Form1和日期文本框的名字Data(加重字体)与脚本是有关的,也就是说你如果改动了它们的名字,凡是在脚本中引用From1和Data的部分都要修改。切切!!! (2)Javascript是大小写敏感的,所以注意大小写的区别和一致性原则。 (3)此脚本应该与CGI/ASP等服务器端的递交处理程序配合使用,用于客户端的合法性检测。本例没有将submit按钮作上去,你所处理的表单中可能包括更多的内容。 这里仅仅提供了一个脚本思路,你不一顶非要全部照搬脚本,可以仅仅取脚本的一部分使用(主要是算法)。