[原创]我的Javascript(二)

转载请注明出处
及其作者feng_sundy

我的Javascript(二)


function String.prototype.trim(){return this.replace(/(^/s*)|(/s*$)/g,"");}
function gf_AppendZero(n){return(("00"+ n).substr(("00"+ n).length-2));}//日期自动补零程序//

//取得当前系统日期时间
function gf_now()  
{
 var fs_now = new Date();
 var fs_return="";
 fs_return = fs_now.getFullYear() + "-" + gf_AppendZero(fs_now.getMonth() + 1) + "-" + gf_AppendZero(fs_now.getDate());
 fs_return +=" " + gf_AppendZero(fs_now.getHours()) + ":" + gf_AppendZero(fs_now.getMinutes()) + ":" + gf_AppendZero(fs_now.getSeconds());
 return fs_return;

//取得当前系统日期
function gf_today()  
{
 var fs_now = new Date();
 var fs_return="";
 fs_return = fs_now.getFullYear() + "-" + gf_AppendZero(fs_now.getMonth() + 1) + "-" + gf_AppendZero(fs_now.getDate());
 return fs_return;

//验证日期是否合法(true/false)
function  gf_chkdate(as_datestr) 

 var fs_year="";fs_mon="";fs_day="";fs_time="";fs_hour="";fs_min="";fs_sec="";fs_obj="";fs_syn="";
 var fi_loop,fi_len
 
 fi_len = as_datestr.length
 fs_obj = "year"
 if (fi_len<=0) return true;
 for (fi_loop=0;fi_loop<fi_len;fi_loop++ )
 {
  if (as_datestr.charAt(fi_loop)==" " && fs_obj == "day" ) fs_obj = "hour";
  if  (as_datestr.charAt(fi_loop)==  '-'|| as_datestr.charAt(fi_loop)==  '/')
  {
   if (fs_syn == "")
   {
    fs_syn = as_datestr.charAt(fi_loop)
   }
   else
   {
    if (fs_syn != as_datestr.charAt(fi_loop)) return false //没有用统一的"-"或"/"符号错误
   } 
   
   if (fs_obj == "year" )
   {
    fs_obj = "month"
   }
   else if (fs_obj == "month" )
   {
    fs_obj = "day"
   }
  }
  else if (as_datestr.charAt(fi_loop)==  ':')
  {
   if (fs_obj == "hour" )
   {
    fs_obj = "minute"
   }
   else if (fs_obj == "minute" )
   {
   fs_obj = "second"
   }
  }
  else //取出相应的值
  {
   if (!(as_datestr.charAt(fi_loop) >= 0 && as_datestr.charAt(fi_loop) <= 9)) return false
   if (fs_obj == "year" )
   {
    fs_year += as_datestr.charAt(fi_loop)
   }
   else if (fs_obj == "month" )
   {
    fs_mon += as_datestr.charAt(fi_loop)
   }
   else if (fs_obj == "day" )
   {
    fs_day += as_datestr.charAt(fi_loop)
   }
   else if (fs_obj == "hour" )
   {
    if (as_datestr.charAt(fi_loop)!= " ") fs_hour += as_datestr.charAt(fi_loop)
   }
   else if (fs_obj == "minute" )
   {
    fs_min += as_datestr.charAt(fi_loop)
   }
   else if (fs_obj == "second" )
   {
    fs_sec += as_datestr.charAt(fi_loop)
   }
  }
 }
    //验证时间的合法性
 if (fs_hour != "")
 {
  if ( fs_min == "" )
  {
   return false
  }
  else
  {
   if (fs_sec == "") fs_sec = "0"
  }
  if ( fs_hour > 24 || fs_hour < 0 || fs_min >= 60 || fs_min < 0 || fs_sec >= 60 || fs_sec < 0 )
  {
   return false //时间错误
  }
  if (fs_hour.length>2 || fs_min.length>2) return false
 }
 //验证日期的合法性
 if ( fs_year == "" || fs_mon=="" || fs_day=="" ) return false;
 if (fs_year.length == 2 )
 {
  fs_year = '20'+ fs_year;
 }
 else if (fs_year.length != 4 )
 {
  return false;
 }
 if ( fs_year.length > 4 || fs_mon.length > 2 || fs_day.length > 2 ) return false;
 if (!(fs_year>1901 && fs_year < 2999 && 1<=fs_mon  && 12>=fs_mon && 31>=fs_day && 1<=fs_day) ) return false;
 if (( fs_mon==4 || fs_mon==6 || fs_mon==9 || fs_mon==11 ) && (fs_day>30)) return false;
 if  ((fs_year % 4 == 0) && (fs_mon==2) && (fs_day > 29))   return false;
 if  (!(fs_year % 4 == 0) && (fs_mon==2) && (fs_day > 28))   return false;

 return true; 
}

//验证只能录入整数onkeypress
function gf_checkint()
{
if ((window.event.keyCode<48 || window.event.keyCode>57) && window.event.keyCode!=45 ) window.event.returnValue=false;
}

//验证只能录入实数onkeypress
function gf_checkobjfloat(objectname) 
{
if ((window.event.keyCode<45 || window.event.keyCode>57 || window.event.keyCode==47) && window.event.keyCode!=45)  window.event.returnValue=false;
if( window.event.keyCode==46 && objectname.value.indexOf(".")>-1) window.event.returnValue=false;
}


//验证不能为空的数据//
function gf_mandatory(as_prompt)

   //as_prompt:tbx_a,不能为空*tbx_b,不能为空*
   var fs_src = as_prompt;
   var fas_p = fs_src.split("*");
   var fas_n;
   for(var i=0;i<fas_p.length;i++)
   {
      if (fas_p[i]!="")
      {
         fas_n = fas_p[i].split(",");
         if (fas_n.length<2){alert("验证表达式有误,请检查");return false;}
         if (document.all(fas_n[0])==null){alert("Control Name:" + fas_n[0] + " is wrong!");return false;}
         if (gf_trim(document.all(fas_n[0]).value)=="")
         {
            alert(fas_n[1]);
            document.all(fas_n[0]).focus();
            return false;
         }
      }
   }
   return true;
}

//检查是否是数字,小数
function gf_chknum(ai_num) 

   var fi_i,fi_j,fi_dot,strTemp; 
   strTemp=".0123456789";
   fi_dot = 0;  //小数点个数
   if (ai_num.toString().length == 0) return false;
   if (ai_num.charAt(0) == "." || ai_num.charAt(ai_num.length - 1) == ".")  return false;
   for(fi_i=0;fi_i<ai_num.length;fi_i++) 
   { 
      fi_j = strTemp.indexOf(ai_num.charAt(i));             
      if (fi_j == -1) return false;
  if (ai_num.charAt(i) == ".")
      {
   fi_dot++;
   if (fi_dot>1) return false;
      }
   } 
   return true; 
}

//去除字符串的空格
function gf_trim(as_string)
{
   while(as_string.length > 0 && as_string.indexOf(" ")==0) as_string = as_string.substr(1);
   while(as_string.length > 0 && as_string.lastIndexOf(" ")==(as_string.length-1)) as_string = as_string.substr(0,as_string.length-1);
   return as_string;
}

//只能输入大写字符
function gf_lc2uc()
{
 if (window.event.keyCode>=97 & window.event.keyCode<=122 )
 {
  window.event.keyCode = window.event.keyCode - 32;
 }
}

/**
格式化日期函数
D--日期2004-12-25
DT--日期时间2004-12-25 12:05:56
**/
function gf_formatdate(as_date,as_datetype)
{
 //2003-11-12 11:25:13
 var fs_tmp = new Array();
 var fs_date = (as_date==null?"":gf_trim(as_date));
 var fs_datetype = (as_datetype==null?"D":"DT");
 if (fs_date=="") return fs_date;
 var fi_space = fs_date.indexOf(" ");
 var fs_ymd = fs_date.substr(0,fi_space);
 var fs_hms = fs_date.substr(fi_space+1);
   if (fs_ymd == "") {fs_ymd = fs_hms;fs_hms = "";}
 fs_tmp = fs_ymd.split("-");
 fs_ymd = (fs_tmp[0].length==2?("20" + fs_tmp[0]):fs_tmp[0]) + "-" + gf_AppendZero(fs_tmp[1]) + "-" + gf_AppendZero(fs_tmp[2]);
 if (fs_hms != "")
 {
    fs_tmp = fs_hms.split(":");
    fs_hms = " " + gf_AppendZero(fs_tmp[0]) + ":" + gf_AppendZero(fs_tmp[1]) + (fs_tmp.length==3?(":" + gf_AppendZero(fs_tmp[2])):"");
 }
 fs_date = fs_ymd + (fs_datetype=="D"?"":fs_hms);
 return fs_date;
}

//集装箱箱号验证
//Author: sundy 20003-4-28
//格式:gf_chkcntrno(as_cntrno, fi_choice ) return( boolean )
//功能:验证集装箱箱号:
//参数:
//   as_cntrno 是否符合国际标准,
//返回值:True 符合国际标准或强行通过(特殊箱号)
//举例:gf_chkcntrno( 'TEXU2982987', 0 )
     
function gf_chkcntrno(as_cntrno,ai_choice)
{
 var fi_ki;
 var fi_numsum;
 var fi_nummod;
 var fai_num = new Array(11);
 var fb_errcntrno=false;

 if (as_cntrno==null) return true; //null不进行验证
 if (gf_trim(as_cntrno)=="") return true; //空不进行验证
 
 if (as_cntrno.length == 11)   //国际标准为11位,最后一位是校验位,若不是11位肯定不是标准箱
 { for(fi_ki=1;fi_ki<=11;fi_ki++)
   fai_num[fi_ki] = 0;
  for(fi_ki=1;fi_ki<=4;fi_ki++)     //根据国际标准验证法则处理箱号前面的4个英文字母
  {
   fch_char=as_cntrno.charAt(fi_ki-1).toUpperCase();
   switch(true)
   { case (fch_char=="A"):{fai_num[fi_ki] = 10;break;}
    case (fch_char>="V" && fch_char<="Z"):{fai_num[fi_ki] = fch_char.charCodeAt() - 52;break;}
    case (fch_char>="L" && fch_char<="U"):{fai_num[fi_ki] = fch_char.charCodeAt() - 53;break;}
    default:{fai_num[fi_ki] = fch_char.charCodeAt() - 54;break;}
   }
  }
  for(fi_ki=5;fi_ki<=11;fi_ki++)
  {  fch_char=as_cntrno.charAt(fi_ki-1);
   fai_num[fi_ki] = parseInt(fch_char); //ctype((mid(as_cntrno, fi_ki, 1)), integer)
      }
  fi_numsum = 0
  
  for(fi_ki=1;fi_ki<=10;fi_ki++)
  { 
   fi_sqr = 1;
   for(i=1;i<fi_ki;i++){fi_sqr *=2;}
   fi_numsum += fai_num[fi_ki] * fi_sqr;
  }

  if (as_cntrno.substr(0,4) == "HLCU") fi_numsum = fi_numsum - 2; //hapaq lloyd的柜号与国际标准相差2
  fi_nummod = fi_numsum % 11;
  if (fi_nummod == 10) fi_nummod = 0;
  if (fi_nummod == fai_num[11]) fb_errcntrno = true;
  return fb_errcntrno;
 }else{
    return fb_errcntrno;
 }  
}


/*由于在Js中Replace只能替换一次,下面函数能全局替换2003.5.13 -sundy
as_expression --包含要替换的字符串
as_find --搜索的子字符串
as_replacement --要替换的字符串
替换字符串函数,如:"'"转换为"''"
*/
function gf_replace(as_expression,as_find,as_replacement)
{
   var fs_expression = as_expression;
   var fs_find = as_find;
   var fs_replacement = as_replacement;
   if (fs_expression == "") return "";
   if (fs_find == "") return "";
   //构造正则表达式/,$,(,),*,+,.,[,?,{,^,|为特殊字符,必须转义后替换
   fs_regx1 = gi;
   fs_find = fs_find.replace(fs_regx1,"
");
   fs_replacement = fs_replacement.replace(fs_regx1,"//");
  
   fs_regx1 = //$/gi;
   fs_find = fs_find.replace(fs_regx1,"
///$");
   fs_replacement = fs_replacement.replace(fs_regx1,"/$");        
   fs_regx1 = //(/gi;
   fs_find = fs_find.replace(fs_regx1,"
///(");
   fs_replacement = fs_replacement.replace(fs_regx1,"/(");        
   fs_regx1 = //)/gi;
   fs_find = fs_find.replace(fs_regx1,"
///)");
   fs_replacement = fs_replacement.replace(fs_regx1,"/)");        
   fs_regx1 = //*/gi;
   fs_find = fs_find.replace(fs_regx1,"
///*");
   fs_replacement = fs_replacement.replace(fs_regx1,"/*");        
   fs_regx1 = //+/gi;
   fs_find = fs_find.replace(fs_regx1,"
///+");
   fs_replacement = fs_replacement.replace(fs_regx1,"/+");
   fs_regx1 = //./gi;
   fs_find = fs_find.replace(fs_regx1,"
///.");
   fs_replacement = fs_replacement.replace(fs_regx1,"/.");
   fs_regx1 = //[/gi;
   fs_find = fs_find.replace(fs_regx1,"
///[");
   fs_replacement = fs_replacement.replace(fs_regx1,"/[");
   fs_regx1 = //?/gi;
   fs_find = fs_find.replace(fs_regx1,"
///?");
   fs_replacement = fs_replacement.replace(fs_regx1,"/?");
   fs_regx1 = //^/gi;
   fs_find = fs_find.replace(fs_regx1,"
///^");
   fs_replacement = fs_replacement.replace(fs_regx1,"/^");
   fs_regx1 = //{/gi;
   fs_find = fs_find.replace(fs_regx1,"
///{");
   fs_replacement = fs_replacement.replace(fs_regx1,"/{");
   fs_regx1 = //|/gi;
   fs_find = fs_find.replace(fs_regx1,"
///|");
   fs_replacement = fs_replacement.replace(fs_regx1,"/|");
   fs_find = "/" + fs_find + "/gi";

   //返回替换后的值
   return fs_expression.replace(eval(fs_find),fs_replacement);
}

/*Jun.14,2003--sundy
字符串转换为数字(""-->0)as_type--str,num
参数说明:as_str--转换的字符串
         ai_digit--转换的小数位数(null--不限制小数位数,0--转换为整型,>0按小数位数转换)
         as_type--转换后返回的类型(null,"num"--转换为数字类型,"str"--转换为字符串(按小数格式化后的字符串)
例如:
gf_str2float("10.2124568795")返回float类型10.2124568795
gf_str2float("10.6",0)返回Int类型11(使用四舍五入的方法)
gf_str2float("10.2",2)返回float类型10.1
gf_str2float("10.2",2,"str")返回String类型"10.20"(按小数位数格式化字符串)
gf_str2float("10.216",2)返回float类型10.22
gf_str2float("10.216",2,"str")返回String类型"10.22"
*/
function gf_str2float(as_str,ai_digit,as_type)
{
   var fdb_tmp = 0;
   var fi_digit = 0;
   var fs_digit = "1";
   var fs_str = "" + as_str;
   var fs_tmp1 = "";
   var fs_tmp2 = "";
   var fi_pos = 0;
   var fi_len = 0;
   fdb_tmp = parseFloat(isNaN(parseFloat(fs_str))?0:fs_str);
  
   switch (true)
   {
      case (ai_digit==null)://不改变值,只转换为数字
         fdb_tmp = fdb_tmp;
         break;
      case (ai_digit==0)://取得整数
         fdb_tmp = Math.round(fdb_tmp);
         break;
      case (ai_digit>0)://按照传入的小数点位数四舍五入取值
         for (var i=0;i<ai_digit;i++) fs_digit +="0";
         fi_digit = parseInt(fs_digit);
         fdb_tmp = Math.round(fdb_tmp * fi_digit) / fi_digit;
         if (as_type=="str")
         {
            fs_tmp1 = fdb_tmp.toString();
            fs_tmp1 +=((fs_tmp1.indexOf(".")!=-1)?"":".") + fs_digit.substr(1);
            fi_pos = fs_tmp1.indexOf(".") + 1 + ai_digit;
            fdb_tmp = fs_tmp1.substr(0,fi_pos);
         }
         break;
   }
   return fdb_tmp;
}

//打印数据
function gf_dgdprint(as_reportname,as_where,as_subtitle,as_footer)
{  //打印datagrid  --sherry 2003-08-19
   /*
   as_reportname--报表格式名  不能为空
   as_where--where 提取条件,形如" From cod_Driver Where Corp ='本单位' 不能为空
   as_subtitle --表头小标题
   as_footer--表尾,
   */
   var fs_Rpt_Property="width=" + (screen.width - 10) + ",height=" + (screen.height - 55) + ",left=0,top=0";
 if (as_reportname==null) as_reportname="";
 if (as_where==null) as_where="";
 if (as_subtitle==null) as_subtitle="";
 if (as_footer==null) as_footer="";
 var S_Rpt_ReportName=escape(as_reportname) ;
 var S_Rpt_Where=escape(as_where) ;
 var S_Rpt_SubTitle=escape(as_subtitle) ;
 var S_Rpt_Footer=escape(as_footer) ;
 var fs_parm = "";
 fs_parm += "?S_Rpt_ReportName=" + S_Rpt_ReportName  ;
 fs_parm += "&S_Rpt_Where=" + S_Rpt_Where ;
 fs_parm += "&S_Rpt_SubTitle=" + S_Rpt_SubTitle ;
 fs_parm += "&S_Rpt_Footer=" + S_Rpt_Footer  ;
   window.open( "../SysPublic/sys_printer.aspx" + fs_parm,"sys_printer",fs_Rpt_Property );
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值