请教高手指点,帮忙写一段检测日期的代码,感激不尽,谢谢~!!!
代码写了一部分但是有点问题,首先逻辑上有点问题,正确的日期仍然提示出错。
然后我希望出错的时候提示出错,禁止表单提交。
还有 比如 "2010-" 这样的输入 竟然可以通过检测,显然有问题。
为了代码简洁,我只要"2010-10-8" 格式,其他一律不通过。
 
参考代码如下:<!--#include file="../Conn3.asp"--><html>
<head>
<title>管理中心</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="../Image/style.css" type=text/css rel=stylesheet>
<script language="JavaScript" > function CheckDate(strInputDate) {
    // 定义一个月份天数常量数组
    var DA = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];    // 统一日期格式
    strDate = strInputDate.replace(/-/g, "/");    //判断日期是否是预期的格式
    if (strDate.indexOf("/") == -1) {
      alert("请输入 yyyy-M-d、yyyy-MM-dd、yyyy/M/d、yyyy/MM/dd 格式。")
      return false;
    }    // 分解出年月日
    arrD = strDate.split("/");
    if (arrD.length != 3) return false;
    y = parseInt(arrD[0], 10);
    m = parseInt(arrD[1], 10);
    d = parseInt(arrD[2], 10);    //判断年月日是否是数字
    if (isNaN(y) || isNaN(m) || isNaN(d)) return false;    // 判断月份是否在1-12之间
    if (m > 12 || m < 1) return false;
    //判断是否是闰年
    if (isLoopYear(y)) DA[2] = 29;    //判断输入的日是否超过了当月月份的总天数。
    if (d > DA[m]) return false;    //各种条件都验证了,则应该是一个合法的日期了。
    // 如果要对日期进行一次格式化,则可以在这里进行处理了,下面格式化成数据库识别的日期格式 yyyy-MM-dd
    // str = y + "-" + (m<10?"0":"") + m + "-" + (d<10?"0":"") + d;
     str = y + "-" + (m < 10 ? "0" : "") + m + "-" + (d < 10 ? "0" : "") + d;
    alert(str)
    return true;
   
  }
  function isLoopYear(theYear) {
    return (new Date(theYear, 1, 29).getDate() == 29);
  } 
function checktuihuo() { 
 if (document.form1.tuihuodate.value=="")
  {
  return true;
  }
//先让空代码 返回 true
   if(CheckDate(form1.tuihuodate.value)){      alert("您输入的退货日期不正确!");form1.tuihuodate.focus();return false;   }
   //checkdate 函数 本身是禁止 空日期的。 checkdate 在后面调用,竟然 空日期也可以通过,真的是不能理解。
   //还有一个小问题。比如我输入2010- 也能通过检测,代码好像不太完美。
  }</SCRIPT>
</head>
<%
tuihuodate=request("tuihuodate")
tuihuodan=request("tuihuodan")
company=request("company")
sheng=request("sheng")
shi=request("shi")
%>
<body text="#000000">
<form onSubmit="return checktuihuo()" name="form1" method="post"   action="">
  <table width="1024" border="1" cellpadding="0" cellspacing="0" align="center" class=TableBorder>
    <tr height="22" valign="middle" align="center"> 
      <th width="14%">退货日期</th>
      <th width="14%">退货单</th>
      <th width="14%">客户名称</th>
      <th width="15%">省份</th>
      <th width="15%">城市</th>
      <th width="28%">&nbsp;</th>
    </tr>
    <tr height="22" valign="middle" align="center"> 
      <td class=forumrow><label>
        <input name="tuihuodate" type="text" id="tuihuodate" value="<%=tuihuodate%>">
      </label></td>
      <td class=forumrow><input name="tuihuodan" type="text" id="tuihuodan" value="<%=tuihuodan%>"></td>
      <td class=forumrow><input name="company" type="text" id="company" value="<%=company%>"></td>
      <td class=forumrow><input name="sheng" type="text" id="sheng" value="<%=sheng%>"></td>
      <td class=forumrow><input name="shi" type="text" id="shi" value="<%=shi%>"></td>
      <td class=forumrow><input type="submit" name="Submit" value="查 找"></td>
    </tr>
  </table>
</form><%'开始分页
Const MaxPerPage=10000
    dim totalPut   
    dim CurrentPage
    dim TotalPages
    dim j
    dim sql
     if Not isempty(request("page")) then
       currentPage=Cint(request("page"))
    else
       currentPage=1
    end if 


set rs=server.CreateObject("adodb.recordset")

sql="select * from tuihuo2 where id>=1"
if tuihuodan<>"" then
sql=sql&"and tuihuodan='"&tuihuodan&"'"
end if
if tuihuodate<>"" then
sql=sql&"and tuihuodate='"&Request("tuihuodate")&"'"
end if 
if company<>"" then
sql=sql&"and company like '%"&company&"%'" 
end if
if sheng<>"" then
sql=sql&"and sheng='"&sheng&"'"
end if
if shi<>"" then
sql=sql&"and shi='"&shi&"'"
end if 
            rs.open sql&" order by id desc",conn,1,1
   

    if err.number<>0 then
response.write "数据库中无数据"
end if

   if rs.eof And rs.bof then        Response.Write "<p align='center' class='contents'> 对不起,没有找到相关信息!</p>"
    else
   totalPut=rs.recordcount       if currentpage<1 then
           currentpage=1
       end if       if (currentpage-1)*MaxPerPage>totalput then
    if (totalPut mod MaxPerPage)=0 then
      currentpage= totalPut \ MaxPerPage
    else
       currentpage= totalPut \ MaxPerPage + 1
    end if
       end if        if currentPage=1 then
             showContent
             showpage totalput,MaxPerPage,"showtuihuo2.asp"
        else
           if (currentPage-1)*MaxPerPage<totalPut then
             rs.move  (currentPage-1)*MaxPerPage
             dim book
             book=rs.book
             showContent
              showpage totalput,MaxPerPage,"showtuihuo2.asp"
         else
         currentPage=1
            showContent
            showpage totalput,MaxPerPage,"showtuihuo2.asp"
       end if
    end if
        end if    sub showContent
        dim i
    i=0%>
<table width="1203" border="1" align="center" cellspacing="0" bgcolor="#ced7f7">
  <tr onMouseOver=mouseOver(this,'999999');   onMouseOut=mouseOut(this,'#ced7f7');>
    <td width="9%" align="center">退货日期</td>
    <td width="7%" align="center">退货单</td>
    <td width="12%" align="center">供应商</td>
    <td width="10%" align="center">产品名称</td>
    <td width="5%" align="center">单位</td>
    <td width="7%" align="center">单价</td>
    <td width="7%" align="center">数量</td>
    <td width="9%" align="center">总额</td>
    <td width="8%" align="center">备注</td>
    <td width="12%" align="center">完成</td>
  </tr>
   <%
   do while not rs.eof%>
  <tr onMouseOver=mouseOver(this,'999999');   onMouseOut=mouseOut(this,'#ced7f7');>
    <td align="center"><a href="#" onClick="javascript:window.open('edittuihuo2.asp?id=<%=rs("id")%>','','scrollbars=no,resizable=no,width=600,height=600,top=100,left=400')">
<%=rs("tuihuodate")%></a></td>
    <td align="center"><a href="edittuihuo2.asp?id=<%=rs("id")%>" target="_blank"><%=rs("tuihuodan")%></a></td>
    <td align="center"><%=rs("company")%></td>
    <td align="center"><%=rs("chanpinname")%></td>
    <td align="center">&nbsp;<%=rs("danwei")%></td>
    <td align="center">&nbsp;<%=rs("danjia")%></td>
    <td align="center">&nbsp;<%=rs("shuliang")%></td>
    <td align="center"><%=rs("zonger")%></td>
    <td align="center">&nbsp;<%=rs("beizhu")%></td>
    <td align="center"></td>
  </tr>
   <%i=i+1
if i>=MaxPerPage then Exit Do
rs.movenext
loop
%>

<%

rs.close
set rs=nothing%>
</table> <% End Sub   
Function showpage(totalnumber,maxperpage,filename)  
   Dim n
  
If totalnumber Mod maxperpage=0 Then  
n= totalnumber \ maxperpage  
Else
n= totalnumber \ maxperpage+1  
End If

Response.Write "<form method=Post action="&filename&"?selectm="&selectm&"&selectkey="&selectkey&" >"  
Response.Write "<p align='center' class='contents'> "  
If CurrentPage<2 Then  
Response.Write "<font class='contents'>首页 上一页</font> "  
Else  
Response.Write "<a href="&filename&"?tuihuodate="&tuihuodate&"&tuihuodan="&tuihuodan&"&company="&company&"&sheng="&sheng&"&shi="&shi&" class='contents'>首页</a> "  
Response.Write "<a href="&filename&"?page="&CurrentPage-1&"&tuihuodate="&tuihuodate&"&tuihuodan="&tuihuodan&"&company="&company&"&sheng="&sheng&"&shi="&shi&" class='contents'>上一页</a> "  
End If

If n-currentpage<1 Then  
Response.Write "<font class='contents'>下一页 尾页</font>"  
Else  
Response.Write "<a href="&filename&"?page="&(CurrentPage+1)&"&tuihuodate="&tuihuodate&"&tuihuodan="&tuihuodan&"&company="&company&"&sheng="&sheng&"&shi="&shi&" class='contents'>"  
Response.Write "下一页</a> <a href="&filename&"?page="&n&"tuihuodate="&tuihuodate&"&tuihuodan="&tuihuodan&"&company="&company&"&sheng="&sheng&"&shi="&shi&" class='contents'>尾页</a>"  
End If  
Response.Write "<font class='contents'> 页次:</font><font class='contents'>"&CurrentPage&"</font><font class='contents'>/"&n&"页</font> "  
Response.Write "<font class='contents'> 共有"&totalnumber&"种商品 " 
Response.Write "<font class='contents'>转到:</font><input type='text' name='page' size=2 maxlength=10 class=smallInput value="&currentpage&">"  
Response.Write "&nbsp;<input type='submit'  class='button' value='GO' name='cndok'></form>"  
End Function  
%></html>

解决方案 »

  1.   

     if(!CheckDate(form1.tuihuodate.value)){  alert("您输入的退货日期不正确!");form1.tuihuodate.focus();return false;  }
      

  2.   

    /** 
    isValidDate: 用于判断一个字符串是否是日期格式的字符串
     
    返回值: 
    true或false
     
    参数: 
    DateString: 判断的字符串 
    Dilimeter : 日期的分隔符,缺省值为'-' 
    */ 
    function isValidDate(DateString , Dilimeter) 

    if (DateString==null) return false;  if (Dilimeter=='' || Dilimeter==null) 
    Dilimeter = '-'; 

    var tempy=''; 
    var tempm=''; 
    var tempd=''; 

    var tempArray; 

    if (DateString.length<8 && DateString.length>10) 
    return false;  

    tempArray = DateString.split(Dilimeter); 

    if (tempArray.length!=3) 
    return false; 
    if (tempArray[0].length==4) 

    tempy = tempArray[0]; 
    tempd = tempArray[2]; 

    else 

    tempy = tempArray[2]; 
    tempd = tempArray[1]; 

    tempm = tempArray[1]; 

    var tDateString = tempy + '/'+tempm + '/'+tempd+' 8:0:0';//加八小时是因为我们处于东八区 

    var tempDate = new Date(tDateString); 
    if (isNaN(tempDate)) 
    return false; 
    if (((tempDate.getUTCFullYear()).toString()==tempy) && (tempDate.getMonth()==parseInt(tempm, 10)-1) && (tempDate.getDate()==parseInt(tempd, 10))) 

    return true; 

    else 


    return false; 

    }