title: Regex to allow only numbers and decimals not working in Javascript
body: <p>I am trying this below code and the regex isn't working and it is allowing all the characters in the input box.</p>
  
  <p>Desired: Input text box should not accept any other character other than numbers and decimal point.</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
      &lt;input type="text" onkeypress="myfunction(event);"&gt;&lt;/input&gt;
  &lt;script&gt;
      function myfunction(e){
           var p = new RegExp(/^[0-9]+([.][0-9]+)?$/);
           return e.charCode === 0 ||   p.test(String.fromCharCode(e.charCode));      
      }
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>