日期:2014-05-16  浏览次数:20428 次

JS左去空格、右去空格和全去空格
<script>
        function trim(str){
                if(str.length < 1){
                        return "";
                }
                str = rtrim(str);
                str = ltrim(str);
                if(str==""){
                        return "";
                }else{
                        return str;
                }
        } 

        function rtrim(str){
                var w_space = String.fromCharCode(32);
                var v_length = str.length;
                var strTemp = "";
                if(v_length < 0){
                        return "";
                }
                var iTemp = v_length -1;

                while(iTemp > -1){
                        if(str.charAt(iTemp) == w_space){
                        }else{
                                strTemp = str.substring(0,iTemp +1);
                                break;
                        }
                        iTemp = iTemp-1;

                } 
                return strTemp;
        } 

        function ltrim(str){
                var w_space = String.fromCharCode(32);
                if(v_length < 1){
                        return"";
                }
                var v_length = str.length;
                var strTemp = "";
                
                var iTemp = 0;
                
                while(iTemp < v_length){
                        if(str.charAt(iTemp) == w_space){
                        }else{
                                strTemp = str.substring(iTemp,v_length);
                                break;
                        }
                        iTemp = iTemp + 1;
                } 
                return strTemp;
        }

        function check(){
                document.login.LoginName.value = trim(document.login.LoginName.value)
                return true;
        }

</script>