title: Regular Expression in JavaScript to allow only numbers with optional 2 decimals
body: <p>I want to write a regular expression in which allows </p>
  
  <ol>
  <li>backspace</li>
  <li>0-9 digits</li>
  <li>optional fractional part with two decimals (no limit on integral part how many digits there can be)</li>
  </ol>
  
  <p>For example:</p>
  
  <ul>
  <li>Allowed lists is [12 , 232.0 ,  23.(with only dot)  , 345.09 , 78.23 ,   134.00  , 0.21  , .21  , .02 , .01 .12 ]</li>
  <li>Not allowed are [12.878 , 34.343.334 , .0003 ]</li>
  </ul>
  
  <p>The use of this regular expression would be like on javascript event</p>
  
  <pre><code>&lt;input type="text"  onKeyPress="validatenumber(event);"   /&gt;&lt;br&gt;
  </code></pre>
  
  <p><b>My code is </b></p>
  
  <pre><code>function validatenumber(evt) {
    var theEvent = evt || window.event;
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode( key );
    var regex = /^[0-9\b]+$/;    // allow only numbers [0-9] 
    if( !regex.test(key) ) {
      theEvent.returnValue = false;
      if(theEvent.preventDefault) theEvent.preventDefault();
    }
  }
  </code></pre>
  
  <p>I want to change only this line with the new regex: </p>
  
  <pre><code>var regex = /^[0-9\b]+$/;    // allow only numbers [0-9]
  </code></pre>