js用法接上

//轉換日期成088/11/11 (ex:輸入881111 -->088/11/11)
function ConvertDate(fld)
{
   flg=true
  
   if (fld.value!=''){
      if (fld.value.substring(0,1)!="1"){
         if (flg==true && (fld.value.length<6 || fld.value.length>9)){
            flg=false;
            alert("請輸入正確的日期,例如:'880808' or '88/08/08'")
   //0709         fld.select()
   fld.value=''
            fld.focus()
         }
      }
     
     //格式化為088/01/01
     if (flg==true && fld.value.length==6){
        //檢查日期
        if (fld.value.indexOf("/",0)>=0){
           flg=false;
           alert("請輸入正確的日期,例如:'880808' or '88/08/08'")
       //0709    fld.select()
       fld.value=''
           fld.focus()
        }
        else{
           if (check_date(fld)){
              StartDATE=fld.value
              a=fld.value.substring(0,fld.value.length-4)             
              if(a.length=2){
                 a='0'+a             
              }  
              //fld.value=fld.value.substring(0,fld.value.length-4)+"/"+fld.value.substring(fld.value.length-4,fld.value.length-2)+"/"+fld.value.substring(fld.value.length-2,fld.value.length)
              fld.value=a+"/"+fld.value.substring(fld.value.length-4,fld.value.length-2)+"/"+fld.value.substring(fld.value.length-2,fld.value.length)
              //alert("fld.value1="+fld.value)
           }else
              flg=false;
        }  
     }else{
        //格式化為088/01/01
        if (flg==true && fld.value.length==7){
           //檢查日期
           if (fld.value.indexOf("/",0)>=0){
              flg=false;
              alert("請輸入正確的日期,例如:'880808' or '88/08/08'")
         //0709     fld.select()
         fld.value=''
              fld.focus()
           }else{
              if (check_date(fld)){
                 StartDATE=fld.value
                 fld.value=fld.value.substring(0,fld.value.length-4)+"/"+fld.value.substring(fld.value.length-4,fld.value.length-2)+"/"+fld.value.substring(fld.value.length-2,fld.value.length)
                 //alert("fld.value1="+fld.value)          
              }else
                 flg=false;
           }  
        }else{
           if (flg==true && fld.value.length==8){
              //檢查日期
              fld.value=fld.value.substring(0,fld.value.length-6)+fld.value.substring(fld.value.length-5,fld.value.length-3)+fld.value.substring(fld.value.length-2,fld.value.length)
              //alert("fld.value="+fld.value)
              if (check_date(fld)){
                 StartDATE=fld.value
                 a=fld.value.substring(0,fld.value.length-4)
                 if(a.length=2){
                   a='0'+a
                 }
                 fld.value=a+"/"+fld.value.substring(fld.value.length-4,fld.value.length-2)+"/"+fld.value.substring(fld.value.length-2,fld.value.length)
                 //alert("fld.value1="+fld.value)             
              }else
                 flg=false;
           }else{
              if (flg==true && fld.value.length==9){
                 //檢查日期
                 fld.value=fld.value.substring(0,fld.value.length-6)+fld.value.substring(fld.value.length-5,fld.value.length-3)+fld.value.substring(fld.value.length-2,fld.value.length)
                 if (check_date(fld)){
                    StartDATE=fld.value
                    fld.value=fld.value.substring(0,fld.value.length-4)+"/"+fld.value.substring(fld.value.length-4,fld.value.length-2)+"/"+fld.value.substring(fld.value.length-2,fld.value.length)
                    //alert("fld.value1="+fld.value)                
                 }else
                    flg=false;
              }
           }
        }
     }
   }
   return flg;
}
// =========================================================================
// 檢查日期是否正確-- 大小月及閏年之判斷 -- (傳入年為西元)
function validate_date(tyear,tmonth,tday) {
 
  if (tmonth == 2 || tmonth == 4 || tmonth == 6 || tmonth == 9 || tmonth == 11 ) { /* 小月 */
     if (tmonth == 2) {
          if (tyear % 4 == 0) { /* 可能是閏年 */
              if (tyear % 100 == 0) { /* 100的倍數 可能是閏年 */
                  if (tyear % 400 == 0) { /* 400的倍數 一定是閏年 */
                      if (tday > 29) return false;
                  } else if (tday > 28) return false;  /* 非400的倍數 一定不是閏年 */
               } else if (tday > 29) return false;   /* 一定是閏年 */
          } else if (tday > 28) return false;   /* 不是閏年 */
     } else if (tday > 30) return false; /* 非2月之小月 */
  }

return true
}

//將傳入的值(txt_str)將<Enter>鍵轉成<br>以利於存到資料庫中
function chg_enter_to_br(txt_str){
   var in_str='';
   var out_str="";
   var chk_char="";

   var in_str=txt_str;
   str_len=in_str.length;
 
   for (i=0;i<str_len;i++){
      chk_char = in_str.substring(i,i+1);
      if (chk_char == '\r'){
         out_str = out_str + ' ';
      }else{
         /* skip \r \n (0D and 0A) */
         if (chk_char != '\n'){
            out_str = out_str + chk_char;
         }
      }
   }
   return out_str
}

posted on 2006-04-11 22:03  freeliver54  阅读(267)  评论(0编辑  收藏  举报

导航