<script language=Javascript>  //自己动手为string添加Trim
function String.prototype.Trim() {return this.replace(/(^\s*)|(\s*$)/g,"");}
function String.prototype.Ltrim(){return this.replace(/(^\s*)/g, "");}
function String.prototype.Rtrim(){return this.replace(/(\s*$)/g, "");}
var str = "  fason    ";
alert(str.Trim());
</script>

解决方案 »

  1.   

    val = val.replace(/(^\s+)|(\s+$)/g,'')
      

  2.   

    meizz的代码
    javascript本没有trim()函数,自己动手打造为string添加Trim
    <script language=Javascript>  //自己动手为string添加Trim
    function String.prototype.Trim() {return this.replace(/(^\s*)|(\s*$)/g,"");}
    function String.prototype.Ltrim(){return this.replace(/(^\s*)/g, "");}
    function String.prototype.Rtrim(){return this.replace(/(\s*$)/g, "");}
    var str = "  meizz    ";
    alert(str.Trim());
    alert(str.Ltrim());
    alert(str.Rtrim());
    </script>
      

  3.   

    通常验证文本框中的值可以通过onSubmit
    <input type="text" name="text1" onKeypress="if(event.keyCode<48||event.keyCode>57) event.returnValue = false;">
      

  4.   

    通常验证文本框中的值可以通过表单的onSubmit事件
    <html>
    <head>
    <title>无标题文档</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <script language=javascript>
    function checktext(){
    if(document.form1.text1.value!="开发"){
    alert("输入有误,请从新输入!");
    document.form1.text1.focus();
    return false;
    }else{
    return true;
    }
    }
    </script>
    </head><body bgcolor="#FFFFFF" text="#000000">
    通常验证文本框中的值可以通过表单的onSubmit事件
    <form name=form1 onSubmit="return checktext();">
    <input type="text" name="text1">
      <input type="submit" name="Submit" value="提交">
    </form></body>
    </html>
      

  5.   

    <script language=vbscript>
    alert(trim(form1.str1.value));
    <script>