首页 > 代码库 > 工作中常用的JavaScript脚本

工作中常用的JavaScript脚本

1.获取字符串字节长度

 1 function GetStrLenByte(str){ 2     var len=0; 3     try{ 4         if (str+"" == "undefined" || str == null){ 5                 len=0; 6         } 7         else if(str.length==0){ 8             len=0; 9         }10         var i;11         for(i=0;i<str.length;i++){12             if(str.charCodeAt(i)>128){13                 len=len+2;14             }15             else{16                 len=len+1;17             }18         }19     }20     catch(Exception Obj0){21     }22 23     return len;24 25 }

2.去处字符串前后空格

 1 function Trim(str){ 2         if (str+"" == "undefined" || str == null){ 3                 return ""; 4         } 5         else if(str.length==0){ 6             return ""; 7         } 8         var i=0; 9         while( str.charAt(i)==‘ ‘){10                 i++;11                 if(i>=str.length){12                     break;13                 }14         }15         var j=str.length-1;16         while( str.charAt(j)==‘ ‘){17                 j--;18                 if(j<0){19                     break;20                 }21         }22         if ( j<i )23             return "";24         else25             return str.substring(i,j+1);26 27 }

3 检查输入参数是否全为数字

 1 function CheckNumber(str){ 2     var rc=true; 3     if (str+"" == "undefined" || str == null){ 4         rc=false; 5     } else if(str.length==0){ 6     rc=false; 7     } else { 8     for(i=0;i<str.length;i++){ 9         if(str.charAt(i)<‘0‘ || str.charAt(i)>‘9‘){10             rc=false;11             break;12         }13     }14     }15     return rc;16 }

4. 检查输入参数是否为整数

 1 function CheckInteger(str){ 2     var rc=true; 3     if (str+"" == "undefined" || str == null){ 4     rc=false; 5     } else{ 6     for(i=0;i<str.length;i++){ 7             if(i==0 && str.charAt(i)==‘-‘){ 8                 continue; 9             }10             if(str.charAt(i)<‘0‘ || str.charAt(i)>‘9‘){11                 rc=false;12                 break;13             }14     }15     }16     return rc;17 }

5.检查输入参数是否为合法的email地址

  1 function CheckEmail(str){  2     var CHAR_LETTER_NUMERIC=1;  3     var CHAR_UNDERLINE=2;  4     var CHAR_DOT=3;  5     var CHAR_AT=4;  6     var CHAR_DIVIDE=5;  7     var CHAR_END=6;  8     var CHAR_OTHER=7;  9     var DIV_CHAR=‘,‘; 10  11     var rc=true; 12     if (str+"" == "undefined" || str == null){ 13         rc=false; 14         return rc; 15     } else if(str.length==0){ 16         rc=true; 17         return rc; 18     } 19  20     var exit_flag=false; 21     var total_char=str.length; 22     var pos=0; 23     var cur_char; 24     var cur_status=0; 25     while((pos<=total_char) && (!exit_flag)){ 26         if(pos==total_char){ 27           cur_char=CHAR_END; 28         } 29         else if (str.charAt(pos)==‘.‘){ 30           cur_char=CHAR_DOT; 31         } 32         else if (str.charAt(pos)==DIV_CHAR){ 33           cur_char=CHAR_DIVIDE; 34         } 35         else if(str.charAt(pos)==‘_‘){ 36           cur_char=CHAR_UNDERLINE; 37         } 38         else if(str.charAt(pos)==‘@‘){ 39           cur_char=CHAR_AT; 40         } 41         else if(((str.charAt(pos)>=‘a‘)&&(str.charAt(pos)<=‘z‘))||((str.charAt(pos)>=‘A‘)&&(str.charAt(pos)<=‘Z‘))||((str.charAt(pos)>=‘0‘)&&(str.charAt(pos)<=‘9‘))||(str.charAt(pos)==‘-‘)){ 42           cur_char=CHAR_LETTER_NUMERIC; 43         } 44         else{ 45           cur_char=CHAR_OTHER; 46         } 47         switch (cur_status){ 48           case -1://error 49             rc=false; 50             exit_flag=true; 51             break; 52  53           case 0://初始状态 54             if((cur_char==CHAR_LETTER_NUMERIC)||(cur_char==CHAR_UNDERLINE)||(cur_char==CHAR_DOT)){ 55               cur_status=1; 56             } 57             else{ 58               rc=false; 59               cur_status=-1; 60             } 61             break; 62           case 1://用户名 63             if((cur_char==CHAR_LETTER_NUMERIC)||(cur_char==CHAR_UNDERLINE)||(cur_char==CHAR_DOT)){ 64               cur_status=1; 65             } 66             else if(cur_char==CHAR_AT){ 67               cur_status=2; 68             } 69             else{ 70               rc=false; 71               cur_status=-1; 72             } 73             break; 74           case 2://@ 75             if(cur_char==CHAR_LETTER_NUMERIC){ 76               cur_status=6; 77             } 78             else{ 79               rc=false; 80               cur_status=-1; 81             } 82             break; 83  84           case 6://"." 85             if(cur_char==CHAR_LETTER_NUMERIC){ 86               cur_status=6; 87             } 88             else if(cur_char==CHAR_DOT){ 89               cur_status=3; 90             } 91             else{ 92               rc=false; 93               cur_status=-1; 94             } 95             break; 96  97           case 3://fisrt domain name 98             if(cur_char==CHAR_LETTER_NUMERIC){ 99               cur_status=4;100             }101             else{102               rc=false;103               cur_status=-1;104             }105             break;106           case 4://not first domain name107             if(cur_char==CHAR_LETTER_NUMERIC){108               cur_status=4;109             }110             else if(cur_char==CHAR_DOT){111               cur_status=3;112             }113             else if(cur_char==CHAR_DIVIDE){114               cur_status=0;115             }116             else if(cur_char==CHAR_END){117               cur_status=5;118             }119             else{120               rc=false;121               cur_status=-1;122             }123             break;124           case 5://ok125             rc=true;126             exit_flag=true;127             break;128           default:129             rc=false;130             exit_flag=true;131             break;132         }133         pos++;134     }135     return rc;136 }

6.判断一个字符串是否以指定字符串开头

1   function startWith(str,start){2     try{3       return (str.substring(0,start.length)==start);4     }catch(ex){5       return false;6     }7   }

7.判断一个字符串是否以指定字符串结尾

1   function endWith(str,end){2     try{3       return (str.substring(str.length-end.length)==end);4     }catch(ex){5       return false;6     }7   }

8.把两个数值相加, 结果为指定精度的相加结果

 1 // num1: 被加数 2 // num2: 加数 3 // float_length:  结果精度(>=0的整数) 4 function addNumber(num1, num2, float_length) { 5    var nTemp = 10; 6    if (float_length==0) { 7      nTemp = 1; 8    } 9   else {10       nTemp = 10*float_length;11   }12 13     var nSum = new Number(num1)*nTemp + new Number(num2)*nTemp;14   nSum = nSum/nTemp;15 16     var sSum = new String(nSum);17     var j = sSum.indexOf(".");18     if (j>=0) {19         sSum = sSum.substring(0, j+float_length+1);20         nSum = new Number(sSum);21     }22 23     return nSum;24 }

9.检查是否为当日以前的合法日期

 1 function IsByPastDate(str1) { 2     if (str1 + "" == "undefined" || str1 == null){ 3         return false; 4     } 5     var y,m,d; 6     var i; 7     i = str1.indexOf("-"); 8     if (i == -1 || i == str1.length) { return false; } 9     y = str1.substring(0, i);10     str1 = str1.substring(i + 1);11 12     i = str1.indexOf("-");13     if (i == -1 || i == str1.length) { return false; }14     m = str1.substring(0, i);15 16     d = str1.substring(i + 1);17 18     return !DateIsLargerThanNow(y,m,d);19 }20 21 //检查日期在当前日期之后22 function DateIsLargerThanNow(year, month, day){23     return DateIsLargerThanNow2(year,month,day,(new Date()));24 }25 26 // 检查日期在给定日期之后27 function DateIsLargerThanNow2(year, month, day, nowdate){28     var rc=false;29     if(!CheckDate(year,month,day)){30         rc=true;31     } else{32         var d1=new Date(Eear,month-1,day);33         var d0=new Date();34         if(d1.getTime()>d0.getTime()){35             rc=true;36         }37     }38     return rc;39 }40 41 // 检查输入参数是否为合法的日期42 function CheckDate(year,month,day){43     if(!CheckNumber(year)){44         return false;45     }46     if(!CheckNumber(month)){47         return false;48     }49     if(!CheckNumber(day)){50         return false;51     }52     var dat = new Date(year, month - 1, day);53     if (dat.getMonth() == (month-1)) {54         return true;55     } else {56         return false;57     }58 }59 60 //检查输入参数是否全为数字61 function CheckNumber(str){62     var rc=true;63     if (str+"" == "undefined" || str == null){64         rc=false;65     } else if(str.length==0){66     rc=false;67     } else {68     for(i=0;i<str.length;i++){69         if(str.charAt(i)<‘0‘ || str.charAt(i)>‘9‘){70             rc=false;71             break;72         }73     }74     }75     return rc;76 }

10 判断日期是否合法,并转换为2011-11-11的格式

 1     function OnRecordTimeBlur() { 2         var recordTime = window.event.srcElement.value; 3         if (recordTime == "") 4             return; 5         if (recordTime.length != 8 && recordTime.length != 10) { 6             window.event.srcElement.focus(); 7             alert(‘日期长度非法!‘); 8             return; 9         }10         if (recordTime.indexOf("-") < 0) {11             recordTime = recordTime.substring(0, 4) + "-"12                     + recordTime.substring(4, 6) + "-" + recordTime.substring(6);13         }14         var recordTime_year = recordTime.substring(0, 4);15         var recordTime_month = recordTime.substring(5, 7);16         var recordTime_day = recordTime.substring(8);17         if (CheckDate(recordTime_year, recordTime_month, recordTime_day) == false) {18             window.event.srcElement.focus();19             alert(‘日期非法!‘);20             return;21         } else {22             window.event.srcElement.value =http://www.mamicode.com/ recordTime;23         }24     }

11.输入的起始期间不能大于终止期间

function checkDate() {        var dateFromStr = document.form1.applyDateFrom.value;        var dateToStr = document.form1.applyDateTo.value;        if (dateFromStr == "" || dateToStr == "") {            return true;        }        var dateFrom = parseDate(dateFromStr);        var dateTo = parseDate(dateToStr);        if (dateFrom > dateTo) {            alert(‘起始期间不能大于终止期间‘);            return false;        }        return true;    }function parseDate(str) {        if (str == "") {            return null;        }        year = str.substring(0, 4);        month = str.substring(5, 7);        day = str.substring(8, 10);        return new Date(year, (month - 1), day);    }

 

 

工作中常用的JavaScript脚本