zoukankan      html  css  js  c++  java
  • 常用简易JavaScript函数(转)

    //函数名:strByteLength 
    //功能介绍:返回字符串的字节长度 
    //参数说明:str    要检查的字符串
    //返回值:字符串长度
    function strByteLength(str) 

        var i,sum; 
        sum=0; 
        for(i=0;i<str.length;i++) 
        { 
            if ((str.charCodeAt(i)>=0) && (str.charCodeAt(i)<=255)) 
                sum=sum+1; 
            else 
                sum=sum+2; 
        }
        return sum;
    }

    //函数名:fucCheckLength 
    //功能介绍:检查表单是否符合规定的长度 
    //参数说明:obj    要检查的表单对象
    //        name   对象名称
    //        length 规定长度
    //返回值:true(符合) or false(不符)  
    function fucCheckLength(obj , name , length) 

        var i,sum; 
        sum=0; 
        var strTemp = obj.value;
        for(i=0;i<strTemp.length;i++) 
        { 
            if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255)) 
                sum=sum+1; 
            else 
                sum=sum+2; 
        }
        if(sum<=length)
        {
            return true;
        }
        else
        {
            alert(name+"超出规定长度!最长允许"+length+"个字符(中文算2位)!");
            obj.focus();
            return false;
        }
    }

    //检测电子邮件是否合法
    function checkEmail(Object)
    {
        var pattern = /^[.-_A-Za-z0-9]+@([-_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/;
        var strValue=Object.value;
        if(strValue.match(pattern)==null){
           alert("Email不合法,请重新填写!");
           Object.focus();
            return false;
         }else{
         return true;
         }
    }


    //去空隔函数
    function Jtrim(str){
        var i = 0;
        var len = str.length;
        if ( str == "" ) return( str );
        j = len -1;
        flagbegin = true;
        flagend = true;
        while ( flagbegin == true && i< len){
            if ( str.charAt(i) == " " ){
                i=i+1;
                flagbegin=true;
            }else{
                flagbegin=false;
            }
        }

        while  (flagend== true && j>=0){
            if (str.charAt(j)==" "){
                j=j-1;
                flagend=true;
            }else{
                flagend=false;
            }
        }

        if ( i > j ) return ("")

        trimstr = str.substring(i,j+1);
        return trimstr;
    }

    //函数名:JtrimCn 
    //功能介绍:去掉字符串前后的空格[包括中文空格]
    //参数说明:str    要操作的字符串
    //返回值:删除了前后空格[包括中文空格]的字符串
    function JtrimCn(str){
        var i = 0;

        if (str == null || str == undefined) {
            return "";
        }

        var len = str.length;
        if ( str == "" ) {
            return( str );
        }
        j = len -1;
        flagbegin = true;
        flagend = true;
        while ( flagbegin == true && i< len){
            if ( str.charAt(i) == " " || str.charAt(i) == " " ){
                i=i+1;
                flagbegin=true;
            }else{
                flagbegin=false;
            }
        }

        while  (flagend== true && j>=0){
            if (str.charAt(j)==" " || str.charAt(j) == " "){
                j=j-1;
                flagend=true;
            }else{
                flagend=false;
            }
        }

        if ( i > j ) {
            return ("")
        }
        var trimstr = str.substring(i,j+1);
        return trimstr;
    }

    //0-9,A-Z,a-z规范字符判断
    function isChar(Str){
        var regu = "^([0-9a-zA-Z]+)$";
        var re = new RegExp(regu);
        if (Str.search(re) != -1){
            return true;
        }
        return false;
    }

    //判断是否数字
    function IsNum(Str){
        var regu = "^([0-9]+)$";
        var re = new RegExp(regu);
        if (Str.search(re) != -1)
            return true;
        {
            return false;
        }
    }

    //函数名:funcIsNotEmpty
    //功能介绍:检查字符串是否为空
    //参数说明:str 字符串
    //返回值:true:不为空    false:为空
    function funcIsNotEmpty(str){
        var s = /\S/;
        if(str==null){
            return false;
        }
        return s.test(str);
    }

    //函数名:fucCheckLength 
    //功能介绍:检查表单是否符合规定的长度 
    //参数说明:objValue    要检查的表单对象的数值
    //        name   对象名称
    //        minLen 最小长度
    //        maxLen 最大长度
    //返回值:true(符合) or false(不符)  
    function fucCheckLengthB(objValue , minLen , maxLen) 

        var i,sum; 
        sum=0; 
        var strTemp = objValue;
        for(i=0;i<strTemp.length;i++) 
        { 
            if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255)) 
                sum=sum+1; 
            else 
                sum=sum+2; 
        }
        if(sum<=maxLen && sum >= minLen)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //sDate1和sDate2是2002-12-18格式 
    function funDateDiff(sDate1, sDate2){ 
        var aDate, oDate1, oDate2, iDays ;
        aDate = sDate1.split("-") ;
        //转换为12-18-2002格式 
        oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); 
        aDate = sDate2.split("-") ;
        oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) ;
        //把相差的毫秒数转换为天数 
        iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24); 
        //如果开始时间小于结束时间
        if (sDate1 > sDate2)
        {
            return (-1 * iDays);
        }
        return iDays;
    }

    //检测电子邮件是否合法
    function funcCheckEmail(strValue)
    {
        var pattern = /^[.-_A-Za-z0-9]+@([-_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/;
        if(strValue.match(pattern)==null){
            return false;
         }else{
         return true;
         }
    }

    //函数名:fucCheckMaxLength
    //功能介绍:检查表单是否符合规定的长度 
    //参数说明:objValue    要检查的表单对象的数值
    //        name   对象数值
    //        maxLen 最大长度
    //返回值:true(符合) or false(不符)  
    function fucCheckMaxLength(objValue , maxLen) 

        return fucCheckLengthB(objValue, 0 ,maxLen );
    }

    //函数名:fucCheckMaxLength
    //功能介绍:检查指定对象的数值是否符合规定的长度 
    //参数说明:objValue    要检查的表单对象的数值
    //        name   对象
    //        maxLen 最大长度
    //返回值:true(符合) or false(不符)  
    function fucCheckObjMaxLength(obj , maxLen) 
    {
        if (obj == null) {
            return false;
        }
        return fucCheckLengthB(obj.value, 0 ,maxLen );
    }

    //函数名:funcCheckInvalidChar
    //功能介绍:判断指定的字段是否有非法字符<>
    //参数说明:obj    要检查的表单对象
    //返回值:true(没有) or false(有)  
    function funcCheckInvalidChar(obj)
    {
        if (obj == null || obj.value== "")
        {
            return true;
        }
        //alert(obj.value);
        var pattern = /[<>]/;
        if(pattern.test(obj.value)){
           return false;
            
         }else{
            return true;
         }
    }

    /**
     * 判断指定的ID的对象的最大长度是否正确
     * param: objId 对象ID
     *        maxLength  最大长度
     * return: true 正确 , false 不正确
     */
    function lengthMaxCheckMsg(objId, maxLength, objName ,needFocus, showMsg) {
        //个人信息check
        var obj = document.getElementById(objId);
        if (!fucCheckObjMaxLength(obj, maxLength)) {
            if (showMsg == null || showMsg== "") {
                alert(objName + "最多只能输入" + (maxLength/2) + "个汉字(或" + maxLength + "个英文数字)");
            } else {
                alert(showMsg);
            }
            if (needFocus) {
                obj.focus();
            }
            return false;
        }
        return true;
    }

    /**
     * 判断指定的ID的对象的是否包含非法字符
     * param: objId 对象ID
     *        objName  对象的名称
     *        needFocus 是否需要设焦点
     *        showMsg 显示的错误消息
     * return: true 正确 , false 不正确
     */
    function invalidCharCheckMsg(objId, objName,needFocus, showMsg) {
        //个人信息check
        var obj = document.getElementById(objId);

        if (!funcCheckInvalidChar(obj)) {
            if (showMsg == null || showMsg== "") {
                alert(objName + '中不能含有“<”或“>”');
            } else {
                alert(showMsg);
            }
            if (needFocus) {
                obj.focus();
            }
            return false;
        }
        return true;
    }

    /**
     * 判断指定的ID的对象是否为空
     * param: objId 对象ID
     *        objName  对象的名称
     *        needFocus 是否需要设焦点
     *        showMsg 显示的错误消息
     * return: true 不为空 , false 为空
     */
    function emptyCheckMsg(objId, objName,needFocus, showMsg) {
        //个人信息check
        var obj = document.getElementById(objId);

        if (!funcIsNotEmpty(obj.value)) {
            if (showMsg == null || showMsg== "") {
                alert(objName + '不能为空!');
            } else {
                alert(showMsg);
            }
            if (needFocus) {
                obj.focus();
            }
            return false;
        }
        return true;
    }

    /*日期计算函数
     * date 2007-01-01
     * cnt  1 or -1
     * return 2007-01-02
     */
    function getDate(date , cnt){
        if(date.length!=10){
            return "";
        }
        var dateArray = date.split("-")
        if(dateArray==null){
            return "";
        }
        var temDate = new Date(dateArray[0], dateArray[1]-1, dateArray[2]);
        var newDate = Date.UTC(temDate.getYear(),temDate.getMonth(),temDate.getDate())
        newDate = newDate + (cnt*24*60*60*1000);
        newDate = new Date(newDate);
        var month = newDate.getMonth()+1;
        var day =  newDate.getDate();
        if(Number(month)<10)
            month = "0"+month;
        if(Number(day)<10)
            day = "0"+day;
        var retDate = newDate.getYear() + "-" + month + "-" + day;
        return retDate;
    }

    //函数名:substringByByte
    //功能介绍:截取字符串
    //参数说明:objValue    要检查的表单对象的数值
    //        length   要截取的长度[字节长度]
    //返回值:截取得到的字符串  
    function funcSubstringByByte(objValue ,length) 

        var i,sum; 
        sum=0; 
        var strTemp = objValue;
        if (strTemp) {
            if (strTemp.length <= (Math.ceil(length / 2))) {
                return strTemp;
            }
            for(i=0;i<strTemp.length && sum <= length ;i++) 
            { 
                if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255)) 
                    sum=sum+1; 
                else 
                    sum=sum+2; 
            }
            
            if (sum > length) {
                return strTemp.substring(0,i - 1);
            } else {
                return objValue;
            }
        }
        return "";
    }

    //函数名:encodeHtml
    //功能介绍:替换关键字
    //参数说明:str    要替换的数据
    //返回值:替换之后的数值 
    function encodeHtml(str){
        if (str) {
            return   str.replace(/>/g,   '&gt;').replace(/</g,   '&lt;').replace(/\'/g,   '&#039;').replace(/\"/g,   '&quot;').replace(/\r/g,   '<br   />');   
        }
        return str;
    }
    //函数名:decodeHTML
    //功能介绍:替换关键字
    //参数说明:str    要替换的数据
    //返回值:替换之后的数值 
    function decodeHTML(val) {
        //alert("val=" + val);
        if (!val) return val;
        var dst = val;
        dst.replace( "&#039;",'\'');
        /**
        dst.replace(/&nbsp;/g, " ");
        dst.replace(/&quot;/g, "\"");
        dst.replace(/&gt;/g, ">");
        dst.replace(/&lt;/g, "<");
        dst.replace(/&amp;/g, "&");
        dst.replace(/&#039;/g, "'");
        **/
        //alert("dst=" + dst);
        return dst;
    }

    http://www.cnblogs.com/S.Sams

    aliyun活动 https://www.aliyun.com/acts/limit-buy?userCode=re2o7acl
  • 相关阅读:
    hdu 1017 A Mathematical Curiosity 解题报告
    hdu 2069 Coin Change 解题报告
    hut 1574 组合问题 解题报告
    hdu 2111 Saving HDU 解题报
    hut 1054 Jesse's Code 解题报告
    hdu1131 Count the Trees解题报告
    hdu 2159 FATE 解题报告
    hdu 1879 继续畅通工程 解题报告
    oracle的系统和对象权限
    oracle 自定义函数 返回一个表类型
  • 原文地址:https://www.cnblogs.com/wangbin/p/1885589.html
Copyright © 2011-2022 走看看