function checkDateFormat(object_value)
    {
    //Returns true if value is a date format or is NULL
    //otherwise returns false
    if (object_value.length == 0)
        return true;
    //Returns true if value is a date in the dd-mm-yyyy format
isplit = object_value.indexOf('/');
if (isplit == -1 || isplit == object_value.length)
return false;
sDay = object_value.substring(0, isplit);
if (sDay.length == 0 || sDay.length >2 )
        return false;

isplit = object_value.indexOf('/', isplit + 1);
if (isplit == -1 || (isplit + 1 ) == object_value.length)
return false;

    sMonth = object_value.substring((sDay.length + 1), isplit);
if (sMonth.length == 0 || sMonth.length > 2)
        return false;

sYear = object_value.substring(isplit + 1);
if (!checkIntegerFormat(sMonth)) //check month
return false;

else
if (!checkNumberRange(sMonth, 1, 12)) //check month
return false;

else
if (!checkIntegerFormat(sYear)) //check year
return false;

else
if (!checkNumberRange(sYear, 1950, 3000)) //check year
return false;

else
if (!checkIntegerFormat(sDay)) //check day
return false;

else
if (!checkday(sYear, sMonth, sDay)) // check day
return false;

else
return true;
    }
function checkday(checkYear, checkMonth, checkDay)
    {
maxDay = 31;
if (checkMonth == 4 || checkMonth == 6 ||
checkMonth == 9 || checkMonth == 11)
maxDay = 30;
else
if (checkMonth == 2)
{
if (checkYear % 4 > 0)
maxDay =28;
else
if (checkYear % 100 == 0 && checkYear % 400 > 0)
maxDay = 28;
else
maxDay = 29;
}
return checkNumberRange(checkDay, 1, maxDay); //check day
    }

解决方案 »

  1.   

    function checkTimeFormat(object_value)
        {
        //Returns true if value is a time format or is NULL
        //otherwise returns false
        if (object_value.length == 0)
            return true;
    isplit1 = object_value.indexOf(':');
    if (isplit1 == -1 || isplit1 == object_value.length)
    return false;
    sHour = object_value.substring(0, isplit1);
    if (sHour.length == 0)
            return false;
    isplit2 = object_value.indexOf(':', isplit1 + 1);
    if ((isplit2 + 1 ) == object_value.length)
    return false;
    if (isplit2 == -1)
    {
    sMinute = object_value.substring(isplit1 + 1);
    sSecond = '00';
    }
    else
    {
    sMinute = object_value.substring((sHour.length + 1), isplit2);
    sSecond = object_value.substring(isplit2 + 1);
    }
    if (sMinute.length == 0 || sSecond.length==0)
           return false;
    /*
    isplit = object_value.indexOf(':');
    if (isplit == -1 || isplit == object_value.length)
    return false;
    sHour = object_value.substring(0, isplit);
    if (sHour.length == 0)
            return false;
    sMinute = object_value.substring(isplit + 1);*/
    if (!checkIntegerFormat(sHour)) //check hour
    return false;
    else
    if (!checkNumberRange(sHour, 0,23)) //check hour
    return false;
    else
    if (!checkIntegerFormat(sMinute)) //check minute
    return false;
    else
    if (!checkNumberRange(sMinute, 0,59)) //check minute
    return false;
    else
    if (!checkIntegerFormat(sSecond))
    return false;
    else
    if (!checkNumberRange(sSecond,0,59))
    return false;
    else
    return true;
        }
      

  2.   

    最简单的用var reg=/^(\d){1,2}\/(\d){1,2}\/(\d){1,4}$/
    alert(reg.test("11/11/1977"));稍有点复杂的请看:http://www.chinavisual.com/?r=viewArticle&id=486
      

  3.   

    function pd(ip,a)
    {
    var re = new RegExp("^([0-9]+):([0-9]+):([0-9]+)$");
    if (re.test(ip))
    {
        var i;
        for (i=1;i<=3;i++)
        {
            var n = eval("RegExp.$"+i);
            if (n < 0 || n > 59)
    {
      alert("时间输入有问题!");
      eval("document.form1."+a).focus();
      eval("document.form1."+a).select();
      return "no";
      }
        }
    }
    else
     {
      alert("时间输入有问题!");
      eval("document.form1."+a).focus();
      eval("document.form1."+a).select();
      return "no";
      }
    }
    function submit1()
    {
    if(pd(document.form1.time1.value,"time1")=="no")
    return false;
    if(pd(document.form1.time2.value,"time2")=="no")
    return false;
    if(pd(document.form1.time3.value,"time3")=="no")
    return false;
    if(pd(document.form1.time4.value,"time4")=="no")
    return false;
    if(pd(document.form1.time5.value,"time5")=="no")
    return false;
    if(pd(document.form1.time6.value,"time6")=="no")
    return false;
    }
      

  4.   

    判断input text 的是否是日期格式
    /*==================================================================*/
    /*  function isDateNull(field) 
    判断input text 的是否是日期格式, 
        接受的格式为yyyy/mm/dd , yyyy-mm-dd. mm/dd/yyyy, mm-dd-yyyy. 
    return: true or false
    /*==================================================================*/function isDate(field){
    /* YYYY/MM/DD or MM/DD/YYYY */
    var exp = trim(field.value);
        var re = new RegExp("/|-");
        var col = exp.split(re);       
        var i, m, d, y;
        var charcode;  
      
    if(exp!="")
    {
    if(field.value.length<3)    return false;   //不合法的日期
        
           switch(col[0].length){                  
            case 4:                         //日期格式为 yyyy-mm-dd
         for(i=0; i<4; i++){
         charcode = col[0].charCodeAt(i);
    if(charcode < 48 || charcode > 57)
    return false;
                          }    
         y = new Number(col[0]); //年份
         m = new Number(col[1]);     //月份
    d = new Number(col[2]);     //日期
                
            if(y <= 1900 || y >=3000){
                 alert("年份必须大于1900年,小于3000年!");
                return false;
                      
                                              }
         if(isNaN(m) || m > 12 || m < 1)
         return false;
         if(isNaN(d) || d > 31 || d <= 0)
         return false;         
    if(col[1].length > 2 || col[2].length > 2 )    
    //日期格式必须为 yyyy-mm-dd
    return false;
         break;
        
         case 1:    
         case 2:                             //日期格式为 mm-dd-yyyy
         for(i=0; i<col[2].length; i++){
         charcode = col[2].charCodeAt(i);
    if(charcode < 48 || charcode > 57)
      return false;
                                            }     
         y = new Number(col[2]);
         m = new Number(col[1]);
         d = new Number(col[0]);    if(y <= 1900 || y >=3000){
                   alert("年份必须大于1900年,小于3000年!");
                    return false;
                                      }
               if(isNaN(m) || m > 12 || m < 1)
         return false;
            if(isNaN(d) || d > 31 || d <= 0)
         return false;              
               
               if(col[1].length>2||col[2].length>2)
    return false;
         break;
         default:
         return false;      
                  }    
        /* check date integrety */            
        
                 if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
                 {
            if(d > 31 || d < 0){          
    return false;
        }     
         }else if( m == 4 || m == 6 || m == 9 || m == 11 ){
        if(d > 30 || d < 0){          
        return false;
        }
         } 
     else if( m == 2)
     {        
        var bYear = false;
       
        if(y % 4 == 0 && y % 100 !=0){
        bYear = true;
                          } 
             else {
        if(y % 400 == 0)
        bYear = true;
        else
                    bYear = false;
                       }
                 if(bYear)
                      {
        if(d > 29)
        return false;
                          } 
                       else {
        if(d > 28)
        return false;
                   }    
         } else 
             {
    return false;
                    }        
        return true;
    }

     
      

  5.   

    VBSCRIPT中有isdata()判断是不是日期
      

  6.   

    alert(isNaN(Date.parse(日期字符串)))