title: Prevent user from entering a non number in a textbox using jQuery
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery">How to allow only numeric (0-9) in HTML  inputbox using jQuery?</a>  </p>
  </blockquote>
  
  
  
  <p>I have a jquery snippet which allows only numbers inside a particular textbox.If the user types a letter/special character, it doesn't allow the character to get into the textbox at all.But it doesn't work.</p>
  
  <pre><code>$("#PostalCode").keydown(function (e) {
              if (e.shiftKey || e.ctrlKey || e.altKey) { // if shift, ctrl or alt keys held down
                  e.preventDefault();         // Prevent character input
              } else {
                  var n = e.keyCode;
                  if (!((n == 8)              // backspace
                          || (n == 46)                // delete
                          || (n &gt;= 35 &amp;&amp; n &lt;= 40)     // arrow keys/home/end
                          || (n &gt;= 48 &amp;&amp; n &lt;= 57)     // numbers on keyboard
                          || (n &gt;= 96 &amp;&amp; n &lt;= 105))   // number on keypad
                          ) {
                      alert("in if");
                      e.preventDefault();     // Prevent character input
                  }
              }
          });
  </code></pre>
  
  <p>The alert message is shown whenever I enter a non-number(letter, or special character), which means that my logic is probably correct. But still the character is displayed inside the textbox, which means that there's something wroing with <code>e.preventDefault()</code>. Can anyone help?</p>