title: Restricting text box inputs to a given regexp using jQuery
body: <p>Consider the following text box:</p>
  
  <pre><code>&lt;input type="text" name="quantity" id="quantity_field" /&gt;
  </code></pre>
  
  <p>Using jQuery I want to restrict the set of  valid inputs into <code>quantity_field</code> by the following regexp:</p>
  
  <pre><code>&lt;script&gt;
    var quantityRegexp = "^(0|[1-9]+[0-9]*)$";
  &lt;/script&gt;
  </code></pre>
  
  <p>More specifically I want to make the browser discard any characters entered into <code>quantity_field</code> that would make the value of the text field not conform to the given regexp. (A corresponding check would of course also be made on the server-side.)</p>
  
  <p>Examples:</p>
  
  <ul>
  <li>If the user types "foo 234" only "234" would get entered in the text box.</li>
  <li>If the user types "001230" only "1230" would get entered in the text box.</li>
  <li>If the user types "foo1bar" only "1" would get entered in the text box.</li>
  </ul>
  
  <p>Question: <b>What is the simplest way to acheive this using jQuery?</b></p>