摘录地址:http://www.mltang.com/article/efd65c8e-07f9-4c9b-8b6b-edab9a9b46a4.html
//Javascript中为String对象添加trim
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, "");}
答3:
<input type=text name=txt size=10><br>
<input type=button value=check onclick="check1()">
<script language="Javascript"><!--
String.prototype.Trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");}
function check1(){
var tt = document.all.txt.value;
var s;
if (tt.Trim().length==0){
alert("输入不能为空!");
document.all.txt.value = "";
return;}
tt = tt.Trim();
for (i = 0; i < tt.length; i++){
s = tt.charCodeAt(i);
if ((s==13) || (s==9)){
alert("你输入的字符中含有tab或者回车键!");
document.all.txt.value = tt;
return; }
}
return;
}
// --></script>