<input type="text" onpropertychange="if(/^[\w]*$/g.test(value)==false) value=value.replace(/[^\w]*/g,'')">

解决方案 »

  1.   

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head>
    <body>
    <label>
    <input type="text" name="textfield"  onkeypress="javascript:return check(this);" /></label>
    </body>
    </html>
    <script language="javascript">
        function check(obj)
        {    
          var c = event.keyCode
             if( c == 13 || (c>=48 && c<=57) || (c>=65 && c<=90) || c==95 || (c>=97 && c<=122) )
                  return true;
                  
             return false;
        }
    </script>
      

  2.   

    <input type="text" name="textfield"  onkeyup="this.value=this.value.replace(/[^\da-z_]/ig,'');"/>