/\d{4}[-]\d{1,2}[-]\d{1,2}/.test(dateString)

解决方案 »

  1.   

    function myGo(theForm)
    {
      var iApplydate = theForm.Applydate.value;
      if(iApplydate.length == 0)
        {
            alert("请填写登记日期 !\n如:2002-03-01");
            return false;
        }
        if (iApplydate.length > 10)
        {
          alert("请正确填写登记日期!\n如:2002-03-01");
          return false;
        }
        for(var j=0;j<iApplydate.length;j++)
        {
          if(iApplydate.charAt(j) == '-')
                jCount++;
          if ((iApplydate.charAt(j) < '0' || iApplydate.charAt(j) >'9' ) && iApplydate.charAt(j) != '-')
          {
            alert("请正确填写登记日期!\n如:2002-03-01");
            return false;
          }
        }
        if(iApplydate.charAt(0) == '-' || iApplydate.charAt(iApplydate.length-1) == '-' || jCount != 2)
        {
            alert("请正确填写登记日期!\n如:2002-03-01");
            return false;
        }    return true;
    }//-->
    </script>
      

  2.   

    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;
         }
    }