我有一段代码,是判断日期格式是否合法。
原来的代码是不允许空日期 和非法日期格式。
现在我想修改一下,当日期为空的时候 就忽略,不进检测 (或者通过检测),当日期格式不为空的时候才进行检测。
请问该如何修改?
会的高手帮忙一下。非常感谢~!!!
代码如下
<script language="javascript">function CheckDate(INDate){ if (INDate==""){return true;}if(INDate.indexOf('-',0)!=-1){ separate="-"}else{   if(INDate.indexOf('/',0)!=-1){separate="/"}   else {return true;}   }   area=INDate.indexOf(separate,0)   //获取年份   subYY=INDate.substr(0,area)   if(isNaN(subYY) || subYY<=0){     return true;   }   //转换月份   subMM=INDate.substr(area+1,INDate.indexOf(separate,area+1)-(area+1))   if(isNaN(subMM) || subMM<=0){     return true;   }   if(subMM.length<2){subMM="0"+subMM}   //转换日   area=INDate.lastIndexOf(separate)   subDD=INDate.substr(area+1,INDate.length-area-1)   if(isNaN(subDD) || subDD<=0){     return true;   }   if(eval(subDD)<10){subDD="0"+eval(subDD)}   NewDate=subYY+"-"+subMM+"-"+subDD   if(NewDate.length!=10){return true;}    if(NewDate.substr(4,1)!="-"){return true;}    if(NewDate.substr(7,1)!="-"){return true;}   var MM=NewDate.substr(5,2);   var DD=NewDate.substr(8,2);   if((subYY%4==0 && subYY%100!=0)||subYY%400==0){ //判断是否为闰年     if(parseInt(MM)==2){           if(DD>29){return true;}     }   }else{     if(parseInt(MM)==2){           if(DD>28){return true;}     }         }   var mm=new Array(1,3,5,7,8,10,12); //判断每月中的最大天数   for(i=0;i< mm.length;i++){     if (parseInt(MM) == mm[i]){           if(parseInt(DD)>31){             return true;           }else{             return false;          }     }   }  if(parseInt(DD)>30){return true;}  if(parseInt(MM)>12){return true;}  return false;  }
function check(){   if(myform.jinhuodate.value==""){      alert("请输入开始日期");myform.jinhuodate.focus();return;   }             if(CheckDate(myform.jinhuodate.value)){      alert("您输入的开始日期不正确!\n 请注意日期格式(如:2007/07/17或2007-07-17)或闰年!");myform.sDate.focus();return;   }   myform.submit();}
</script>

解决方案 »

  1.   

    function check(){   if(CheckDate(myform.jinhuodate.value)){  alert("您输入的开始日期不正确!\n 请注意日期格式(如:2007/07/17或2007-07-17)或闰年!");myform.sDate.focus();return;  }  myform.submit();}
      

  2.   

    谢谢 aspwebchh 的帮助。不过没有答对哦~!
      

  3.   

    你不是说要 去掉验证空的功能吗只要把 check函数中  验证为空的那段去掉就行了
    <script language="javascript">function CheckDate(INDate){  if (INDate==""){return true;}if(INDate.indexOf('-',0)!=-1){ separate="-"}else{  if(INDate.indexOf('/',0)!=-1){separate="/"}  else {return true;}  }  area=INDate.indexOf(separate,0)  //获取年份  subYY=INDate.substr(0,area)  if(isNaN(subYY) || subYY<=0){  return true;  }  //转换月份  subMM=INDate.substr(area+1,INDate.indexOf(separate,area+1)-(area+1))  if(isNaN(subMM) || subMM<=0){  return true;  }  if(subMM.length<2){subMM="0"+subMM}  //转换日  area=INDate.lastIndexOf(separate)  subDD=INDate.substr(area+1,INDate.length-area-1)  if(isNaN(subDD) || subDD<=0){  return true;  }  if(eval(subDD)<10){subDD="0"+eval(subDD)}  NewDate=subYY+"-"+subMM+"-"+subDD  if(NewDate.length!=10){return true;}  if(NewDate.substr(4,1)!="-"){return true;}  if(NewDate.substr(7,1)!="-"){return true;}  var MM=NewDate.substr(5,2);  var DD=NewDate.substr(8,2);  if((subYY%4==0 && subYY%100!=0)||subYY%400==0){ //判断是否为闰年  if(parseInt(MM)==2){  if(DD>29){return true;}  }  }else{  if(parseInt(MM)==2){  if(DD>28){return true;}  }     }  var mm=new Array(1,3,5,7,8,10,12); //判断每月中的最大天数  for(i=0;i< mm.length;i++){  if (parseInt(MM) == mm[i]){  if(parseInt(DD)>31){  return true;  }else{  return false;  }  }  }  if(parseInt(DD)>30){return true;}  if(parseInt(MM)>12){return true;}  return false;  }
    function check(){   if(CheckDate(myform.jinhuodate.value)){  alert("您输入的开始日期不正确!\n 请注意日期格式(如:2007/07/17或2007-07-17)或闰年!");myform.sDate.focus();return;  }  myform.submit();}
    </script>
      

  4.   

    只需要改函数的第一句就好了。function CheckDate(INDate){ 
        if (INDate==""){return true;}
    -->
    function CheckDate(INDate){ 
        if (INDate==""){return false;}
    这段代码写得相当繁琐,应该是刚毕业的学生写的。
      

  5.   

    我晕,  是按照网上的教程抄的。
    谢谢 zswang 的帮助。
    但是我刚才 忘记 补充了。
    我总共有两个日期。搜索的时候  搜索 日期jinhuodate 到 日期jinhuodate2 之间的数据。当两个日期都是空的时候 说明查询者 不限制日期范围。所以日期为空的时候 直接通过检测。
    当输入任何一个日期的时候,就对日期格式进行检测,合法才能通过。
    并且不能只输入一个日期。请问这样的代码该如何写???
    有劳大家了~~!!!
      

  6.   

    可以这样简化:
    function CheckDate(text) {
    if (!text) return false;
    text = text.replace(/[\/-]0?/g, "/");
    if (!text.match(/^\d{4}\/\d{1,2}\/\d{1,2}$/)) return true;
    var d = new Date(text);
    return [d.getFullYear(), d.getMonth() + 1, d.getDate()].join("/") != text;
    }document.open();
    var datas = ["", "2010-01-01", "2010/02/30", "2010/02", "2010/22/30", "2012/2/29", "2012/02/29"];
    for(var i = 0; i < datas.length; i++) {
    document.write(datas[i], "=", CheckDate(datas[i]), "<br/>");
    }
    document.close();输出:
    =false
    2010-01-01=false
    2010/02/30=true
    2010/02=true
    2010/22/30=true
    2012/2/29=false
    2012/02/29=false
      

  7.   

    本帖最后由 zswang 于 2010-05-30 00:06:26 编辑
      

  8.   

    谢谢~!!!
    谢谢 zswang~!!! 人长得帅 技术也很帅~!!!