在Js中判断某一变量是否为空时,如检验登录信息的用户名,密码等,需要去掉空格,其中包括去掉右,左空格,还包括去掉所有空格,Js实现如下:
1
<script>
2
function doclick()
{
3
var tt = " 1234 fdsef ";
4
String.prototype.Trim = function()
5

{
6
return this.replace(/(^\s*) |(\s*$)/g, ""); //去掉左右空格
7
8
}
9
10
String.prototype.LTrim = function()
11

{
12
return this.replace(/(^\s*)/g, ""); // 去掉左空格
13
14
}
15
16
String.prototype.RTrim = function()
17

{
18
return this.replace(/(\s*$)/g, ""); //去掉右空格
19
20
}
21
22
String.prototype.TrimAll = function()
23

{
24
return this.replace(/\s+/g,""); //去掉所有空格
25
26
}
27
28
alert(tt.Trim());
29
alert(tt.LTrim());
30
alert(tt.RTrim());
31
alert(tt.TrimAll());
32
</script>
posted on 2008-07-18 21:19
henry1451 阅读(627)
评论(1) 编辑 收藏