 <p>Make your HTML like this:</p> <pre><code>&lt;input type="text" name="mytextbox" id="mytextbox" onKeyUp="countChar(this)" size="1" maxlength="1" value="" /&gt; </code></pre> <p>And JS will be this:</p> <pre><code>function countChar(that) { var xy = that.value; var len = xy.length; if (len &gt;= 2) { that.value = that.value.substring(0, 1); } else { that.value=(1 - len); } } </code></pre>