title: Allow only digit, comma or dot in the user input in JS
body: <p>I have a user input where only a certain set of characters is allowed. That is, user should be able to enter only a number, possibly with floating point. User may use either comma or dot as a separator. </p>
  
  <p>I've decided to implement this using JS replace function. My approach is to replace everything that doesn't match the set with empty string. </p>
  
  <p>I've sorted out the regex to match the set pretty quickly, here it is: </p>
  
  <pre><code>^\d+[,.]{0,1}\d+$
  </code></pre>
  
  <p>I know it's probably not ideal, but it's quite okay since there is a server-side validation anyway. </p>
  
  <p>However, no matter how hard I tried, I was not able to figure out how to replace anything that doesn't match this regex with empty string. </p>
  
  <p>Here's how I use replace:</p>
  
  <pre><code>var cleanInputOut = function (element) {
      element.value = element.value.replace(/&lt;RegEx goes here&gt;/g, '')
  }
  </code></pre>
  
  <p>I am probably doing it wrong. I would be okay even with quite simple functionality - simply replace any non-digit, not comma and not dot with empty string. I've tried negative lookahead in regex but unfortunately I was not able to make it work as I want to. </p>