(转)js去空格

<script language="javascript" type="text/javascript">
function checkName(){
   
    String.prototype.Trim   =   function()   //给string增加个Trim()方法
{  
return   this.replace(/(^\s*)|(\s*$)/g,   "");  
}  
   
String.prototype.LTrim   =   function()    //给string增加个LTrim()方法,去左边的空格

{  
return   this.replace(/(^\s*)/g,   "");  
}  
   
String.prototype.RTrim   =   function() // 给string增加个RTrim()方法,去右边的空格

{  
return   this.replace(/(\s*$)/g,   "");  
}   


String.prototype.len = function()           // 给string增加个len ()方法,计算string的字节数
{
    return this.replace(/[^\x00-\xff]/g, "xx").length;
}


    var obj=document.getElementById("txtContent").value;   //取得textbox里的数据
    var count=obj.Trim().len();                                              //去点空格后计算字节数
    if(count<1||obj=="请填写评论的内容!")
    {
       document.getElementById("U_name_Err1").className="show";
        document.getElementById("U_name_Err2").className="unshow";
       return false;
    }
     if(count>1000)
    {
       document.getElementById("U_name_Err2").className="show";
       document.getElementById("U_name_Err1").className="unshow";
       return false;
    }
    else
    {
       document.getElementById("U_name_Err2").className="unshow";
       document.getElementById("U_name_Err1").className="unshow";
    }
    return true;
}
</script>

原文地址:https://www.cnblogs.com/s021368/p/1648658.html