代码改变世界

js functions

2009-07-13 08:48  zhangbaoyu  阅读(311)  评论(1编辑  收藏  举报

function ckNum(text,type)
    {
        if(event.keyCode==9 || event.keyCode==37 || event.keyCode==37)return
        s=text.value
        while(s.indexOf("0")!=-1){s=s.replace("0","0");}
        while(s.indexOf("1")!=-1){s=s.replace("1","1");}
        while(s.indexOf("2")!=-1){s=s.replace("2","2");}
        while(s.indexOf("3")!=-1){s=s.replace("3","3");}
        while(s.indexOf("4")!=-1){s=s.replace("4","4");}
        while(s.indexOf("5")!=-1){s=s.replace("5","5");}
        while(s.indexOf("6")!=-1){s=s.replace("6","6");}
        while(s.indexOf("7")!=-1){s=s.replace("7","7");}
        while(s.indexOf("8")!=-1){s=s.replace("8","8");}
        while(s.indexOf("9")!=-1){s=s.replace("9","9");}
        while(s.indexOf(",")!=-1){s=s.replace(",",",");}
        while(s.indexOf("-")!=-1){s=s.replace("-","-");}
        while(s.indexOf(" ")!=-1){s=s.replace(" "," ");}
        while(s.indexOf(".")!=-1){s=s.replace(".",".");}
        while(s.indexOf("_")!=-1){s=s.replace("_","_");}
        while(s.indexOf("+")!=-1){s=s.replace("+","+");}
        num=s.replace(/([^0-9.]+)/gi,"")
        var strtemp=num.replace(/\.{2}/gi,"")
        if(text.value!=strtemp)text.value=strtemp
        if(type=="")
        {
            if(text.value=="")text.value=0;
        }
        else
        {
            if(text.value=="")text.value="";
        }
    }

 

function getAge(value)
    {
        var str=value;
        var len=str.length;
        var age=0;
        if (len==15)//15位身份证
        {
            var s=str.substr(len-1,1);
            //if ((str.substr(len-1,1)%2)!=0)//判断顺序码是否是奇数
                //sex.value="男";
            //else
                //sex.value="女";
            var date1=new Date();//取得当前日期
            var year1=date1.getFullYear();//取得当前年份
            var month1=date1.getMonth();//取得当前月份
            //if (month1>parseInt(str.substr(8,2)))//判断当前月分与编码中的月份大小
                //age=year1-("19"+str.substr(6,2));
            //else
                //age=year1-("19"+str.substr(6,2))-1;
            age=year1-("19"+str.substr(6,2));
        }
        if (len==18)//18位身份证
        {
            //if ((str.substr(len-2,1)%2)!=0)//判断顺序码是否是奇数
                //sex.value="男";
            //else
                //sex.value="女";
            var date1=new Date();//取得当前日期
            var year1=date1.getFullYear();//取得当前年份
            var month1=date1.getMonth();//取得当前月份
            //if (month1>parseInt(str.substr(10,2)))//判断当前月分与编码中的月份大小
                //age=year1-str.substr(6,4);
            //else
                //age=year1-str.substr(6,4)-1;
            age=year1-str.substr(6,4);
        }
        //age=parseInt(age)+1;
        return age;
    }

 

String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g, "");}
        String.prototype.ltrim=function(){return this.replace(/(^\s*)/g,"");}
        String.prototype.rtrim=function(){return this.replace(/(\s*$)/g,"");}
        Number.prototype.format=function(decimalPoints,thousandsSep,decimalSep){
         var val=this+'',re=/^(-?)(\d+)/,x,y;
         if (decimalPoints!=null) val = this.toFixed(decimalPoints);
         if (thousandsSep && (x=re.exec(val))){
          for (var a=x[2].split(''),i=a.length-3;i>0;i-=3) a.splice(i,0,thousandsSep);
          val=val.replace(re,x[1]+a.join(''));
         }
         if (decimalSep) val=val.replace(/\./,decimalSep);
         return val;
        }
        if (typeof Number.prototype.toFixed!='function' || (.9).toFixed()=='0' || (.007).toFixed(2)=='0.00') Number.prototype.toFixed=function(f){
         if (isNaN(f*=1) || f<0 || f>20) f=0;
         var s='',x=this.valueOf(),m='';
         if (this<0){ s='-'; x*=-1; }
         if (x>=Math.pow(10,21)) m=x.toString();
         else{
          m=Math.round(Math.pow(10,f)*x).toString();
          if (f!=0){
           var k=m.length;
           if (k<=f){
            var z='00000000000000000000'.substring(0,f+1-k);
            m=z+m;
            k=f+1;
           }
           var a = m.substring(0,k-f);
           var b = m.substring(k-f);
           m = a+'.'+b;
          }
         }
         if (m=='0') s='';
         return s+m;
        }

 

function formatInteger(src, pos)
    {       
          switch (pos)
          {
              case -1:
                  var str1 = ("" + src).replace(/(\.\d+)/, "");
                  return str1
          }
      }