This code sort of works, but it still allows values like this one: “0.96+66.69.96” and “-.”. Any ideas on how to limit it to JUST one decimal point after an operator with a number? I basically just want the second rule (must be “an operator plus a number AND the last char shouldn´t be a decimal) to be strict. I think the Regex isn´t working properly, but I don´t understand why.
EDIT: The problem, after doing some console logging, is that the check always happens before I enter a number (for instance, if I enter 0.” the last number before the decimal was a zero, not a .). Any idea on how I could fix it?
I think you must fix the regex with the $
operator . This specifys the group of characters requery at end.
Thank you for the reply. There´s an unnecesary escape character before the dot. This sort of works, but it still won´t let me do “9.45+45.6”. It blocks any decimal character after the pattern…I want it to allow decimals ONLY when the pattern is found and/or when there´s none present.
. ↩︎
Which one do you mean?
I would change the focus of the regex. The only way to prevent the chain from ending up in point is to combine the ^ (beginning) and $ (end) operators. I think you should use more methods in a single line. The simplest solution is the conditional you have shown from the beginning.