您的位置:首页 - Javascript
js正则去字符串前后空格的例子

效果体验:http://keleyi.com/keleyi/phtml/javascript/1.htm

js掉字符串前后空格的函数例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试去空格-柯乐义</title>
<script type="text/javascript">
//http://keleyi.com/a/bjad/80d54vgs.htm
//Trim() , Ltrim() , RTrim() 函数
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, "");
}

function testTrim()
{
var name;
name = document.keleyiform.uname.value.Trim();
//name = document.keleyiform.uname.value.RTrim();
//name = document.keleyiform.uname.value.LTrim();
if (name.length == 0) {
alert("用户明不能为空!" + name.length);
document.myform.uname.select();
return false;
}
else {
alert(name.length);
}

}

</script>
</head>
<body>
<form id="keleyiform" name="keleyiform" method="get" action="">
<label>
<input type="text" name="uname" />
</label>
<label>
<input type="submit" name="Submit" value="提交" onclick="return testTrim()" />
</label>
</form>
</body>
</html>
本站导航
相关文章
柯乐义 Copyright© keleyi.com