js 時間的檢查

  • 2323
  • 0
  • js
  • 2010-07-16

摘要:js 時間的檢查

貼上submit會卡住日期<當天 的code

var today = new Date();
var myDate=new Date(CurForm.EndDate.value);
if (myDate<today){tmpMsg = tmpMsg + "[公告迄日] 不能小於當日\n";}

貼上程式blur用來檢查日期格式code(errTextBox =null 是解除欄位互鎖用的)

errTextBox =null;
len = this.value.length;
if(len > 0){
 if(len != 10){
  alert("日期格式有錯");
   errTextBox = this;
  this.focus();
 }else{
  var yr = this.value.substring(0,4);
  var dash1 = this.value.substring(4,5);
  var mn = this.value.substring(5,7);  
  var dash2 = this.value.substring(7,8);
  var dy = this.value.substring(8,10);  
  
  if(dash1 != "/"){
   alert("日期格式有錯");
   errTextBox = this;
   this.focus();
  }else if(dash2 != "/"){
   alert("日期格式有錯");
   errTextBox = this;
   this.focus();
  }else{
   if(isNaN(yr) || isNaN(mn) || isNaN(dy)){
    alert("日期格式有錯");
    errTextBox = this;
    this.focus();
   }else{
    TheDate = new Date(this.value);
    TheMonth = TheDate.getMonth();
    if(mn != (TheMonth+1)){
     alert("不合法的日期");
     errTextBox = this;
     this.focus();                          
                         }    
   }
  }
 }
}