Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>With jQuery</h2> <p><strong>Update</strong>: I saw the other answers using jQuery and assumed you had jQuery. Read through below for notes, and then see below for the non jQuery versions.</p> <p>Also, my example uses <code>\d</code> which allows <code>0</code> as well (your question explicitly states <code>1-9</code>). If you really do want to limit <code>1-9</code>, swap <code>\d</code> with <code>1-9</code>.</p> <h3>Example 1</h3> <p>You could just strip the non valid characters on <code>keyup</code>.</p> <pre><code>$('textarea').keyup(function() { $(this).val(function(index, oldVal) { return oldVal.replace(/[^\d-]/g, ''); }); }); </code></pre> <p><a href="http://jsfiddle.net/alexdickson/tREYz/" rel="noreferrer">jsFiddle</a>.</p> <p>You may also want to bind other events, such as <code>paste</code>.</p> <h3>Example 2</h3> <p>If the character displaying momentarily is annoying you, you could use this...</p> <pre><code>$('textarea').keypress(function(event) { var keyCode = event.keyCode; if ( ! (keyCode &gt;= 48 &amp;&amp; keyCode &lt;= 57) &amp;&amp; keyCode != 45) { event.preventDefault(); } }); </code></pre> <p><a href="http://jsfiddle.net/alexdickson/L3WxQ/" rel="noreferrer">jsFiddle</a>.</p> <h3>Example 3</h3> <p>If that is too difficult to read (with key codes), you could do this...</p> <pre><code>$('textarea').keypress(function(event) { if ( ! String.fromCharCode(event.keyCode).match(/[\d-]/)) { event.preventDefault(); } }); </code></pre> <p><a href="http://jsfiddle.net/alexdickson/38ccP/" rel="noreferrer">jsFiddle</a>.</p> <p>Also, when browsers finally catch up to the spec, you could use the <a href="http://dev.w3.org/html5/spec-author-view/common-input-element-attributes.html#the-pattern-attribute" rel="noreferrer"><code>pattern</code> attribute</a> with <code>[\d-]+</code>. This will only allow <code>0-9</code> and <code>-</code>.</p> <hr> <h2>JavaScript</h2> <h3>Example 1</h3> <pre><code>var textareas = document.getElementsByTagName('textarea'); for (var i = 0, textareasLength = textareas.length; i &lt; textareasLength; i++) { var textarea = textareas[i]; textarea.onkeyup = function() { textarea.value = textarea.value.replace(/[^\d-]/g, ''); } } </code></pre> <p><a href="http://jsfiddle.net/alexdickson/m4kWj/" rel="noreferrer">jsFiddle</a>.</p> <h3>Example 2</h3> <pre><code>var textareas = document.getElementsByTagName('textarea'); for (var i = 0, textareasLength = textareas.length; i &lt; textareasLength; i++) { var textarea = textareas[i]; textarea.onkeypress = function(event) { var keyCode = event.keyCode || event.which; if (!(keyCode &gt;= 48 &amp;&amp; keyCode &lt;= 57) &amp;&amp; keyCode != 45) { event.preventDefault(); } } } </code></pre> <p><a href="http://jsfiddle.net/alexdickson/M7FZV/" rel="noreferrer">jsFiddle</a>.</p> <h3>Example 3</h3> <pre><code>var textareas = document.getElementsByTagName('textarea'); for (var i = 0, textareasLength = textareas.length; i &lt; textareasLength; i++) { var textarea = textareas[i]; textarea.onkeypress = function(event) { var keyCode = event.keyCode || event.which; if (!String.fromCharCode(event.keyCode).match(/[\d-]/)) { event.preventDefault(); } } } </code></pre> <p><a href="http://jsfiddle.net/alexdickson/unJGM/" rel="noreferrer">jsFiddle</a>.</p>
 

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