CSDN博客

img huagou

根据指定的格式验证日期时间

发表于2004/7/12 12:49:00  579人阅读

<script language="javascript">
//参数dt:日期时间字符串
//参数msg:控件描述
//参数fm:日期时间格式
//参数type:语种类型,如空:公元,1:min
function checkDateByMask(dt,msg,fm,type){

//1.定义特殊的格式
var N=10;
var format=new Array(N);
format[0]="yyyy/MM/dd";
format[1]="yyyy-MM-dd";
format[2]="yy/MM/dd";
format[3]="yy-MM-dd";

format[4]="yyyy/MM/dd hh:mm:ss";
format[5]="yyyy-MM-dd hh:mm:ss";
format[6]="yy/MM/dd hh:mm:ss";
format[7]="yy-MM-dd hh:mm:ss";

format[8]="hh:mm:ss";
format[9]="hh:mm";

//2.定义错误提示
msg="/'"+msg+"/'";
var errPar=msg+"参数有误";
var errFormat=msg+"请按如下格式输入:/n"+fm;
var errYear=msg+"年份不对";
var errMonth=msg+"月份不对";
var errDay=msg+"日期不对";
var errHour=msg+"小时不对";
var errMinute=msg+"分钟不对";
var errSecond=msg+"秒钟不对";

  
//3.判断参数是否正确
var b=false;
for(var i=0;i<N;i++){
     if(format[i].toLowerCase()==fm.toLowerCase()){
            b=true;break;
     }
}
if(!b){
     return getErrorMsg(errPar);
}

//4.补足位数
if(dt.length!=fm.length){
     var dt4=dt.replace(/[^0-9]/g,",")
     var dtarr=dt4.split(",");
     var dt3="";
     var dtlen=0;
     for(var i=0;i<dtarr.length;i++){
         var len=dtarr[i].length;
         dtlen=dtlen+len+1;
         if(len<1)
                dt3=dt3+"00"+dtarr[i];
         else if(len<2)
                dt3=dt3+"0"+dtarr[i];
         else
                dt3=dt3+dtarr[i];
               
         dt3=dt3+dt.substr(dtlen-1,1);
         }
     dt=dt3;
}
//5.判断时间字符串与格式参数是否相符
if(dt.length!=fm.length){        
     return getErrorMsg(errFormat);
}
else{
     var dt1=dt.replace(/[0-9]/g,"%d");
     var dt2=fm.replace(/[ymdhs]/gi,"%d");
     //alert(dt1+"/n"+dt2);
     if(dt1!=dt2){
           return getErrorMsg(errFormat);
     }
}    
        
//5.判断日期时间是否正确
try{
     fm=fm.replace(/Y/g,"y").replace(/D/g,"d");
     var iyyyy=fm.indexOf("yyyy");
     var iyy=fm.indexOf("yy");
     var imm=fm.indexOf("MM");
     var idd=fm.indexOf("dd");
     var ihh=fm.indexOf("hh");
     var imi=fm.indexOf("mm");
     var iss=fm.indexOf("ss");
   
     var newdt=new Date();
    
     var year="";
     //Year   
     try{
         var isyear=false;
         if(iyyyy>-1){               
            year=dt.substr(iyyyy,4);
            isyear=true;
         }
         else if(iyy>-1){
            year=dt.substr(iyy,2);
            isyear=true;
         }
         if(isyear){
            if(type=="1"){//
               year=parseInt(year)+1911;
            }
            newdt.setFullYear(year);
         }  
     }
     catch(e1){
         return getErrorMsg(errYear+e1.toString());
     }
    
     //Month
     try{    
         if(imm>-1){
             if(dt.substr(imm,2)>"12"||dt.substr(imm,2)<"01"){
                 return getErrorMsg(errMonth);
             }
             newdt.setMonth(dt.substr(imm,2)-1);
         }
     }
     catch(e1){
         return getErrorMsg(errMonth+e1.toString());
     }
    
     //Day
     try{    
         if(idd>-1){
             if(dt.substr(idd,2)>"31"||dt.substr(idd,2)<"01"){
                 return getErrorMsg(errDay);
             }
             newdt.setDate(dt.substr(idd,2)); 
         }
     }
     catch(e1){
         return getErrorMsg(errDay);
     }
    
     //Hour
     try{
         if(ihh>-1){
             if(dt.substr(ihh,2)>"23"){
                 return getErrorMsg(errHour);
             }
             newdt.setHours(dt.substr(ihh,2));
         }
     }
     catch(e1){
         return getErrorMsg(errHour);
     }
    
     //Minute
     try{
         if(imi>-1){
             if(dt.substr(imi,2)>"59"){
                 return getErrorMsg(errMinute);
             }
             newdt.setMinutes(dt.substr(imi,2));
         }
     }
     catch(e1){
         return getErrorMsg(errMinute);
     }
    
     //Second
     try{
         if(iss>-1){
             if(dt.substr(iss,2)>"59"){
                 return getErrorMsg(errSecond);
             }
             newdt.setSeconds(dt.substr(iss,2));
         }        
     }
     catch(e1){
         return getErrorMsg(errSecond);
     }
         
     //Year
     if(iyyyy>-1){
          if(newdt.getFullYear()!=year){
                 return getErrorMsg(errYear);
          }
     }
     else if(iyy>-1){
          if(newdt.getFullYear()!=year){
                 return getErrorMsg(errYear);
          }
     }
    
     //Month
     if(imm>-1){
          if(newdt.getMonth()!=(dt.substr(imm,2)-1)){
                 return getErrorMsg(errDay);
          }
     }
    
     //Day      
     if(idd>-1){
          if(newdt.getDate()!=dt.substr(idd,2)){
                 return getErrorMsg(errDay);
          }
     }   
           

     //Hour
     if(ihh>-1){
          if(newdt.getHours()!=dt.substr(ihh,2)){
                 return getErrorMsg(errMinute);
          }
     }
    
     //Minute
     if(imi>-1){
          if(newdt.getMinutes()!=dt.substr(imi,2)){
                 return getErrorMsg(errSecond);
          }
     }
    
     //Second
     if(iss>-1){         
          if(newdt.getSeconds()!=dt.substr(iss,2)){
                 return getErrorMsg(errSecond);
          }
     }
     alert(newdt);

catch(e){
     return getErrorMsg(e.toString());
}

           
}

function getErrorMsg(msg){
     alert(msg);
     return false;
     }

function  isGyDate(obj,msg){
     if(!checkDateByMask(obj.value,msg,"yyyy/MM/dd",""))
         obj.focus();    
     }
function  isMgDate(obj,msg){
     if(!checkDateByMask(obj.value,msg,"yy/MM/dd","1"))
         obj.focus();    
     }
    
function  isGyDateTime(obj,msg){
     if(!checkDateByMask(obj.value,msg,"yyyy/MM/dd hh:mm:ss",""))
         obj.focus();    
     }
function  isMgDateTime(obj,msg){
     if(!checkDateByMask(obj.value,msg,"yy/MM/dd hh:mm:ss","1"))
         obj.focus();    
     }  
      
function  isTime(obj,msg){
     if(!checkDateByMask(obj.value,msg,"hh:mm:ss",""))
         obj.focus();    
     }
    
function checkDate(){
    return checkDateByMask(document.all.aa.value,"aa",document.all.formatStr.value,"");
}
</script>


<form name="form1">
<select name="formatStr">
<option value="yyyy/MM/dd">yyyy/MM/dd</option>
<option value="yyyy-MM-dd">yyyy-MM-dd</option>
<option value="yy/MM/dd">yy/MM/dd</option>
<option value="yy-MM-dd">yy-MM-dd</option>

<option value="yyyy/MM/dd hh:mm:ss">yyyy/MM/dd hh:mm:ss</option>
<option value="yyyy-MM-dd hh:mm:ss">yyyy-MM-dd hh:mm:ss</option>
<option value="yy/MM/dd hh:mm:ss">yy/MM/dd hh:mm:ss</option>
<option value="yy-MM-dd hh:mm:ss">yy-MM-dd hh:mm:ss</option>

<option value="hh:mm:ss">hh:mm:ss</option>
<option value="hh:mm">hh:mm</option>
</select>
<input type="text" name="aa" size="20"><br>

<input type="button" name="log1" value="Submit" onclick="checkDate()">

</form>

0 0

相关博文

我的热门文章

img
取 消
img