http://www.csdn.net/expert/topic/712/712631.xml?temp=.4607355
http://www.csdn.net/expert/topic/747/747366.xml?temp=.7035944

解决方案 »

  1.   

    <script language="javascript">
    var s="2002-4-16"
    alert(chkDate(s));
    function chkDate(sDate){
    var r=/\d{4}(?:-\d{1,2}){0,2}/
    //正则表达式,判断是否为yyyy-mm-dd,yyyy-mm,yyyy格式
    if(sDate.match(r)==sDate){
    var arr=sDate.split("-")
    switch(arr.length){
    //根据不同的yyyy-mm-dd,yyyy-mm格式判断年月日数字是否正确
    case 3:
    var tmpDate=new Date(arr[0],arr[1],arr[2]);
    if(tmpDate.getMonth()==arr[1] && tmpDate.getFullYear()==arr[0]) return true;
    break;
    case 2:
    if(arr[1]<13) return true;
    break;
    default:
    return false;
    }
    }
    return false;
    }
    </script>
      

  2.   

    问题已经解决。我是参照“ fokker(独孤龙) ”大侠为我提供的方法做的,如下所示:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <title>日期合法性判断</title>
    <script language="javascript">
    function chkDate(sDate)
    {
    for (i=0;i<sDate.length;i++)
    {
    sDate=sDate.replace("/","-");
    }
    var r=/\d{4}(?:-\d{1,2}){0,2}/
    //正则表达式,判断是否为yyyy-mm-dd,yyyy-mm,yyyy格式
    if(sDate.match(r)==sDate)
    {
    var arr=sDate.split("-")
    switch(arr.length)
    {
    //根据不同的yyyy-mm-dd,yyyy-mm格式判断年月日数字是否正确
    case 3:
    var tmpDate=new Date(arr[0],arr[1],arr[2]);
    if(tmpDate.getMonth()==arr[1] && tmpDate.getFullYear()==arr[0]) return true;
    break;
    case 2:
    if(arr[1]<13) return true;
    break;
    case 1:
    if(arr[0].length==4) return true;
    break;
    default:
    return false;
    }
    }
    return false;
    }
    </script>
    </head>
    <body>
    <input type="text" name="txtdate"  size="20"><br>
    <input type="button" name="btnchkdate" onclick="if (chkDate(document.all.txtdate.value)==false) {alert('输入的日期不合法!');document.all.txtdate.select();document.all.txtdate.focus();} else  alert ('输入的日期合法。');" value="CheckDate">
    </body>
    </html>