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.
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.
I've sorted out the regex to match the set pretty quickly, here it is:
^\d+[,.]{0,1}\d+$
I know it's probably not ideal, but it's quite okay since there is a server-side validation anyway.
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.
Here's how I use replace:
var cleanInputOut = function (element) {
element.value = element.value.replace(/<RegEx goes here>/g, '')
}
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.