title: Javascript Regex - sanitize string with whitelist
body: <p>I have a very basic RegEx problem.  I'm trying to sanitize an input field with a whitelist.  I'm trying to only allow numbers and a decimal into my field.  If a user types an invalid character, I want to strip it out of the input and replace the input with a clean string.</p>
  
  <p>I can get it working with only numbers, but I can't get the decimal into the allowed pool of characters:</p>
  
  <pre><code>var sanitize = function(inputValue) {
      var clean = "",
          numbersOnly = /[^0-9]/g;  // only numbers &amp; a decimal place
      if (inputValue) {
          if (numbersOnly.test(inputValue)) { 
              // if test passes, there are bad characters
              for (var i = 0; i &lt; inputValue.length; i++) {
                  clean += (!numbersOnly.test(inputValue.charAt(i))) ? inputValue.charAt(i) : "";
              }
              if (clean != inputValue) {
                  makeInputBe(clean);
              }
          }
      }
  };
  </code></pre>
  
  <p>Working <a href="http://jsfiddle.net/scott_in_ct/Q8es8/" rel="noreferrer">fiddle</a></p>