<script type="text/javascript">

         function addUser() {
  var userIdField = document.getElementById("userId");

   if (trim(userIdField.value) == "" ) {
 alert("用户代码不能为空!");
 return;
   }

 }

                </script>   <form name="form1" method="POST" action="--WEBBOT-SELF--">      <input  name=userId type="text" id="userId">
    <input  type="button" value="添加" onClick="addUser()">

 </form>

解决方案 »

  1.   

    trim在js里木有
     if (!(/\S+/).test(userIdField.value)) {
                 alert("用户代码不能为空!");        
                 return;
               }
      

  2.   

    另外送你一个trim方法
    String.prototype.trim = function() {
    return this.replace(/^\s*/,"").replace(/\s*$/,"");
    }//注册了之后以后字符串都可以用trim了var i = " asdf  ";
    alert(i.trim().length);
      

  3.   


    String.prototype.trim = function() {
    return this.replace(/(?:^\s+)|(?:\s+$)/g,'');
    }alert('1'+('  xxxx  '.trim())+'2');
      

  4.   

    if(userIdField.value.trim().length==0) alert('输入不合法');