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.
I can get it working with only numbers, but I can't get the decimal into the allowed pool of characters:
var sanitize = function(inputValue) {
var clean = "",
numbersOnly = /[^0-9]/g; // only numbers & a decimal place
if (inputValue) {
if (numbersOnly.test(inputValue)) {
// if test passes, there are bad characters
for (var i = 0; i < inputValue.length; i++) {
clean += (!numbersOnly.test(inputValue.charAt(i))) ? inputValue.charAt(i) : "";
}
if (clean != inputValue) {
makeInputBe(clean);
}
}
}
};
Working fiddle
inputValue.replace(/[^0-9.,]+)/,'');Number.isFloat(inputValue) || Number.isInteger(inputValue)