javascript去除空格

//去除中间空格
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, "");
}

 

使用:

if(name.LTrim().RTrim().length == 0){
alert("请输入用户名");
return;
}

posted on 2012-12-23 11:33  老茶壶  阅读(142)  评论(0编辑  收藏  举报