<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>replaceHref</title>
</head><body>
 <script >
function cc(){
  if(/(^ *| *$)/.test(document.getElementsByName("aaa")[0].value)){
     alert(" :have")
 return false;
  }
}
</script>
<form   name= "form1 "     method= "post "   action= "http://www.csdn.net "  onsubmit="return cc()" >
<input   name= "aaa"   type= "text "   />
<input   type=submit   name=button   value= "bbb "   />
</form>
</body></html>另 name不要用id这种关键字

解决方案 »

  1.   

    <html>
    <head></head>
    <script>
    function chk(f)
    {
      var id=f.id.value;
      if(id=="")
      {
        alert("id不能为空!");
        f.id.focus();
        return false;
      }
      if(id.charAt(0)==" "||id.charAt(id.length-1)==" ")
      {
        alert("id前面或者后面不能有空格!");
        f.id.focus();
        return false;
       }  
    }
    </script>
    <body>
    <form name="form1" method="post" action= "http://www.csdn.net" onsubmit="return chk(this)"> 
    <input   name="id"   type="text"   /> 
    <input   type=submit   name=button   value="提交"   /> 
    </form> </body>
    </html>