Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is something I made another time for just numbers, it will allow all the formatters as well.</p> <h2>jQuery</h2> <pre><code>$('input').keypress(function(e) { var a = []; var k = e.which; for (i = 48; i &lt; 58; i++) a.push(i); if (!(a.indexOf(k)&gt;=0)) e.preventDefault(); });​ </code></pre> <h2>Try it</h2> <p><a href="http://jsfiddle.net/zpg8k/" rel="noreferrer">http://jsfiddle.net/zpg8k/</a></p> <p>As a note, you'll want to filter on submit/server side as well, for sake of pasting/context menu and browsers that don't support the paste event.</p> <h2>Edit to elaborate on multiple methods</h2> <p>I see you're bouncing around the 'accepted' answer, so I'll clear something up. You can really use any of the methods listed here, they all work. What I'd personally do is use mine for live client side filtering, and then on submit and server side use RegEx as suggested by others. However, <strong>no client side by itself will be 100% effective</strong> as there is nothing stopping me from putting <code>document.getElementById('theInput').value = 'Hey, letters.';</code> in the console and bypassing any clientside verification (except for polling, but I could just cancel the <code>setInterval</code> from the console as well). Use whichever client side solution you like, but be sure you implement something on submit and server side as well.</p> <h2>Edit 2 - @Tim Down</h2> <p>Alright, per the comments I had to adjust two things I didn't think of. First, keypress instead of keydown, which has been updated, but the lack of indexOf in IE (seriously Microsoft!?) breaks the example above as well. Here's an alternative </p> <pre><code>$('input').keypress(function(e) { var a = []; var k = e.which; for (i = 48; i &lt; 58; i++) a.push(i); if (!($.inArray(k,a)&gt;=0)) e.preventDefault(); });​ </code></pre> <p>New jsfiddle: <a href="http://jsfiddle.net/umNuB/" rel="noreferrer">http://jsfiddle.net/umNuB/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload