效果體驗:http:///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:///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>