if (isNaN(Date.parse("MM/DD/YYYY"))){}
if (isNaN(Date.UTC(year, month, day))) {}

解决方案 »

  1.   

    还有不太正统的做法:
    if ((Date.parse("MM/DD/YYYY")+"")=="NaN" ){}

    if ((Date.UTC(year, month, day))+"")=="NaN") {}
      

  2.   

    呵呵,楼上的方法很简单,也很实用。我这有段代码,可以判断用户的输入是否符合日期格式。你稍做改动,就可以使用了:function verifyDate(textObj){
      var tmpDateValue = textObj.value;
      var tmpLength = tmpDateValue.length;
      if (tmpLength == 0){
        return true;
      }
      for (var i = 0; i < tmpLength;i++){
        aChar = tmpDateValue.substring(i,i+1);
        if(aChar != "-" && (aChar < "0" || aChar > "9")) {
          alert ("请按照格式输入日期(yyyy-mm-dd)。");
          textObj.focus(this);
          textObj.select(this);
          return false;
        }
      }
      if ((tmpLength < 8 || tmpLength > 10) && tmpLength != 0) {
        alert ("请按照格式输入日期(yyyy-mm-dd)。");
          textObj.focus(this);
          textObj.select(this);
          return false;  
      }
      for (var j= 0; j < 4;j++){
        aChar = tmpDateValue.substring(j,j+1);
        if(aChar < "0" || aChar > "9") {
          alert ("请按照格式输入日期(yyyy-mm-dd)。");
          textObj.focus(this);
          textObj.select(this);
          return false;  
        }
      }
      if (tmpDateValue.substring(4,5) != "-" || tmpDateValue.substring(5,6) == "-"){
        alert ("请按规定格式输入日期(yyyy-mm-dd)。");
        textObj.focus(this);
        textObj.select(this); 
        return false;
      
      }
      if (tmpLength == 8){
        if (tmpDateValue.substring(6,7) != "-" || tmpDateValue.substring(7,8) == "-" ){
          alert ("请按规定格式输入日期(yyyy-mm-dd)。");
          textObj.focus(this);
          textObj.select(this); 
          return false;
        }
      }
      
      if (tmpLength == 9){
        if (tmpDateValue.substring(8,9) == "-" ){
          alert ("请按规定格式输入日期(yyyy-mm-dd)。");
          textObj.focus(this);
          textObj.select(this); 
          return false;    
        }   
      }
      
      if (tmpLength == 10){
        if (tmpDateValue.substring(7,8) != "-" || tmpDateValue.substring(6,7) == "-" || tmpDateValue.substring(8,9) == "-" || tmpDateValue.substring(9,10) == "-" ){
          alert ("请按规定格式输入日期(yyyy-mm-dd)。");
          textObj.focus(this);
          textObj.select(this); 
          return false;    
        }   
      }
      var count=0;
      for (var k = 0; k < tmpLength;k++){
        aChar = tmpDateValue.substring(k,k+1);
        if(aChar == "-") {
            count++;
        }
      }
      if (count!=2){
        alert("请按照格式输入日期!(yyyy-mm-dd)");
        textObj.focus(this);
        textObj.select(this);
        return false;
      
      }
      return true;
    }
      

  3.   

    我觉得用户界面应该友好些
    把年月日给用户选择<select>那样出错的机会会小很多。。至少格式上不会出错。。
      

  4.   

    谢谢chenzengxi(懒猫)与vincentmax(天地任逍遥),由其是chenzengxi(懒猫)方法,我就陷进NaN这个旋涡之中,vincentmax(天地任逍遥)方法我也考虑过,但是太麻烦,不太实用。不过对于linhaibo(美洲豹),有人用select选择2-30日期不也是错误的吗?