Regex for input type="number" prevent enter plus character
I have a Regex for validation input type="number" but the problem with my regex that it allows to enter plus character, how to prevent enter plus symbol, but allow to enter minus? For example you can enter: 123; 11,22; -33; -33.44 but not allowed enter +123 and -0, +0
const deprecatedKeys = /^-+?\D$|^decimal$|^multiply$|^add$|^divide$|^subtract$|^spacebar$/i;
Solution 1:
The 1st part of your current pattern is problematic: ^-+?\D$
would currently match an hyphen between one and unlimited times but as few as possible and a non-digit character. So stuff like '-----+' or '-X' would match too.
If you want to allow for an optional leading hyphen and optional decimals, you could use:
^(?!-0$)-?\d+(?:[.,]\d+)?$
Furthermore, all these alternations could be grouped in a non-capture group with a single leading and trailing anchor:
^(?:(?!-0$)-?\d+(?:[.,]\d+)?|decimal|multiply|add|divide|subtract|spacebar)$