title: Which is the proper way of filtering numeric values for a text field?
body: <p>I'm working on a textfield working with the kind of validation that wouldn't let you enter other than numeric values. As so, my initial code looked quite simple and similar to this:</p>
  
  <pre><code>$(textField).onKeyPress(function(e) {
      if (e.which &lt; 48 &amp;&amp; e.which &gt; 57)
          e.preventDefault();
  });
  </code></pre>
  
  <p>This is fairly strightforward, but turns that (in the latest version of all browsers) Firefox will make this also prevent movement with the arrow keys and delete/backspace keys, whereas the other browsers would not.</p>
  
  <p>Looking around I found that I would need to also check for these keys, and check for different properties exposed in the <code>e</code> event reference.</p>
  
  <p>My final code looks something like this:</p>
  
  <pre><code>$(textField).onKeyPress(function(e) {
      var code = e.which || e.keyCode;
      if (code &gt; 31  // is not a control key
          &amp;&amp; (code &lt; 37 || code &gt; 40) // is not an arrow key
          &amp;&amp; (code &lt; 48 || code &gt; 57) // is not numeric
          &amp;&amp; (code != 46) // is not the delete key
      )
          e.preventDefault();
  });
  </code></pre>
  
  <p>However, this feels to be too much to solve a fairly simple problem as just preventing non-numeric.</p>
  
  <p><strong>What am I doing wrong? Which is the best practice in terms of this kind of validation?</strong></p>