How can I use jQuery to prevent a user from typing too many decimal places?
<p>I'm trying to write a jQuery plugin to prevent users from typing numbers with more than 2 decimal places. Specifically:</p>

<ul>
<li>If an input contains <code>12</code> and the user types <code>3</code> at the end, it should work.</li>
<li>If an input contains <code>12.34</code> and the user types <code>1</code> at the end, <strong>nothing should happen</strong>.</li>
<li>If an input contains <code>12.34</code> and the user types <code>1</code> at the beginning, it should work.</li>
</ul>

<p>Here's the problem I'm running into:</p>

<ul>
<li>If I bind to <code>keypress</code>, I do not know what the "proposed new value" is; <code>$(this).val()</code> is the value before the user pressed the key, and I don't know where in the input field the user is typing.</li>
<li>If I bind to <code>keyup</code>, <code>$(this).val()</code> is the new value, but it has already appeared in the text input. I can erase it if it has too many decimal places, but it looks glitchy.</li>
</ul>

<p>How can I do this?</p>
