我可以提供一个思路
设y,m,d分别是年,月,天
可以用year,month,day函数得到字符串的年,月,天,然后用+把3个变量连起来,和原来的字符串比较,如相等则输入正确,不等输入错误。
1999/1/32得到的年月日分别是1999,2,1这样拼起来的1999/2/1!=1999/1/32
这样就知道是错误的了

解决方案 »

  1.   

    试试这段代码:
    var SearchStr=/^(\d{4})-(\d{1,2})-(\d{1,2})$/g;
             var obj=form名字.文本框名字
    if(obj.value.search(SearchStr))
    {
    alert("请输入正确的日期!");
    obj.focus();  
    return false;
    }
    else
    {
    var temp=SearchStr.exec(obj.value)
    if(RegExp.$2>12 || RegExp.$2<1)
    {
    alert("请输入正确的月份数字!");
    obj.focus();  
    return false;
    }
    else
    {
    switch(RegExp.$2*1)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
    if(RegExp.$3*1>31)
    {
    alert("请输入正确的日期数字!");
    obj.focus();  
    return false;
    }
    break;
    case 4:
    case 6:
    case 9:
    case 11:
    if(RegExp.$3*1>30)
    {
    alert("请输入正确的日期数字!");
    obj.focus();  
    return false;
    }
    break;
    case 2:
    if(RegExp.$1*1 % 4==0)
    {
    if(RegExp.$3*1>29)
    {
    alert("请输入正确的日期数字!");
    obj.focus();  
    return false;
    }
    }
    else
    {
    if(RegExp.$3*1>28)
    {
    alert("请输入正确的日期数字!");
    obj.focus();  
    return false;
    }
    }
    break;
    }
    }
      

  2.   

    直接将日期的格式定死,年月日都有下拉选择的,然后在JS中判断是否合法:如1999-2-29就不合法 var year=document.frm.yy.value; 
    var month=document.frm.mm.value; 
    var day=document.frm.dd.value;

    if (month=="2") 

    if((year%4==0 && year%100==0)|| (year%400==0)) 

    if(day>29) 

    alert("日期不对呀!!!");
    return false;


    else 

    if(day>28) 

    alert("日期不对呀!!!");
    return false;
     } 


    if((month==4)||(month==6)||(month==9)||(month==11)) 

    if(day>30) 

    alert("日期不对呀!!!");
    return false;

    }
      

  3.   

    //------功能:判斷日期是否正确----參數:str--字符串----------返回:true--日期合法,false--日期不正確
    function CheckDate(str)
        {
          var newdate;
          var strdate,stra,month;
          var err=false;
          var islarge=false;
          newdate = new Date(str);
          if (newdate=='NaN'  &&  str.length!=0)
               {
                 err=true;
               }
          else
               {
                 if(str.length!=0)
                 {
                 stra = str.split(" ");
                 strdate = stra[0].split("/");
                 if (strdate[0].length !=4)
                     err=true;
                 if ((strdate[0] % 400 ==0) || ((strdate[0] % 100 != 0 )&&(strdate[0] % 4 == 0)))
                      islarge=true;
                      month=strdate[1];
                 if ((month > 12) && (month < 0))
                         err=true;
                 if ((month==1||month==3||month==5||month==7||month==8||month==10||month==12) && (strdate[2]>31))
                         err=true;
                 if ((month == 2) && (islarge==true) && (strdate[2] > 29))
                         err=true;
                 if ((month == 2) && (islarge==false) && (strdate[2] > 28))
                         err=true;
                 if (((month==4)||(month==6)||(month==9)||(month==11))&&(strdate[2]>30))
                         err=true;
                 if ((month==0) ||(strdate[2]==0))
                         err=true;
                  }
               }
          if (err==true)
             return false;
          else
            return true; }
    function  dateright(){
    if (! CheckDate(frmhistory.RegDate.value)){//form中的RegDate值
          alert( "請輸入正確的日期格式:yyyy/MM/dd");
          frmhistory.RegDate.value="";
          }
    }
      

  4.   

    不过要用onblur="vbscript:dateright()"调用.
      

  5.   

    用JavaScript 调用
    VBScript  IsDate(MyDate)函数
    该函数返回T或F
      

  6.   

    一般情況下我的做法是允許別人用如"2001/7/56"來輸入﹐因為有時這樣的輸入方式也是有用的。
    function inputok(hello)
    {
    var a
    try{
    a=new Date(hello.value);
    //如果不允許空的話就執行以下的if句
    if (isNaN(a)) {alert("請按以下格式輸入日期\nYYYY/MM/DD");
    hello.focus();
    return false;}
    hello.value=a.getFullYear()+"/"+(a.getMonth()+1)+"/"+a.getDate();
    }
    catch (Exception) {alert("輸入日期不正確");hello.focus();return false;}
    return true;
    }
      

  7.   

    不好意思﹐我剛才寫錯了.
    function inputok(hello)
    {
    var a
    try
    { if (hello.value.length>0)//如果不允許為空的話就不要這句判斷
    {
    a=new Date(hello.value);
    if (isNaN(a)) {alert("請按以下格式輸入日期\nYYYY/MM/DD");return false;}
    hello.value=a.getFullYear()+"/"+(a.getMonth()+1)+"/"+a.getDate();
    }
    }
    catch (Exception) {alert("輸入格式不正確");return false;}
    return true;
    }