<input name="aaa" onblur="if(/[  ]/.test(this.value)){alert('错误');this.value=''}">
<input name="aaa" onblur="this.value=this.value.replace(/[  ]/g,'')">

解决方案 »

  1.   

    L@_@K<!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>
      <title>clear space</title>
      <meta name="generator" content="editplus" />
      <meta name="author" content="[email protected]" />
      <meta name="keywords" content="javascript" />
      <meta name="description" content="for javascript region of csdn" />
     </head> <body>
      <input type="text" id="txtInput" />
      <input type="button" id="btnCheck" value="Check" />
     </body>
     <script type="text/javascript">
     <!--
    String.prototype.clearSpace = function()
    {
        return this.replace(/\s*/g, "");
    }var oText = document.getElementById("txtInput");
    var oCheck = document.getElementById("btnCheck");oText.onkeydown = function() {
        if (event.keyCode==32)
        {
            event.returnValue = false;
        }
    };oCheck.onclick = function() {
        oText.value = oText.value.clearSpace()
    }; //-->
     </script>
    </html>
      

  2.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>new page</title>
    <script>
    function a(obj){
    if(event.keyCode==32){
    obj.value=obj.value.replace(/[\s ]/g,"");
    }
    }
    </script>
    </head>
    <body>
    <input type=text value="" onkeyup="a(this)">
    </body>
    </html>