title: Best way to restrict a text field to numbers only?
body: <p>I'm using the following Javascript to restrict a text field on my website to only accept numerical input, and no other letters or characters. The problem is, it REALLY rejects all other key inputs, like <kbd>ctrl</kbd>-<kbd>A</kbd> to select the text, or even any other browser functions like <kbd>ctrl</kbd>-<kbd>T</kbd> or <kbd>ctrl</kbd>-<kbd>W</kbd> while the text box is selected. Does anyone know of a better script to only allow numerical input, but not block normal commands (that aren't being directly input into the field)? Thanks
  Here is the code I'm using now:</p>
  
  <pre><code>function numbersonly(e, decimal) 
  {
      var key;
      var keychar;
  
      if (window.event) 
          key = window.event.keyCode;
      else if (e) 
          key = e.which;
      else 
          return true;
  
      keychar = String.fromCharCode(key);
  
      if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27))
         return true;     
      else if ((("0123456789").indexOf(keychar) &gt; -1))
         return true;
      else if (decimal &amp;&amp; (keychar == "."))
         return true;        
      else
         return false;
  }
  </code></pre>
  
  <p>Edit: None of the solutions provided have solved my problem of allowing commands like <kbd>ctrl</kbd>-<kbd>A</kbd> while the text box is selected. That was the whole point of my asking here, so I have gone back to using my original script. Oh well.</p>