title: Restrict text box to two decimal places in Javascript
body: <p>I am trying to restrict a text box to two decimal places using JavaScript. The field also cannot start with a Period.</p>
  
  <pre><code>jQuery.fn.ForceCurrencyOnly = function () {
  return this.each(function () {
      $(this).keypress(function () {
  
          //Only allow period and numbers
          if ((event.which != 46 || $(this).val().indexOf('.') != -1) &amp;&amp; (event.which &lt; 48 || event.which &gt; 57)) {
              event.preventDefault();
          }
  
          //Prevent a period being entered first
          else if (event.which == 46 &amp;&amp; $(this).val().length == 0) {
             event.preventDefault();
          }
  
          //Only two numbers after a decimal
          else if (($(this).val().indexOf('.') != -1) &amp;&amp; ($(this).val().substring($(this).val().indexOf('.'), $(this).val().indexOf('.').length).length &gt; 2)) {
             event.preventDefault();
          }
      });
  });
  </code></pre>
  
  <p>};</p>
  
  <p>The above restricts the input to numbers, a single period and a maximum of two decimal places. My problem is once 100.00 is entered, the value cannot be highlighted and removed with a number. It has to be deleted.</p>
  
  <p>Is there a simpler way of only allowing a currency with two decimal places?</p>
  
  <p>Any help appreciated.</p>