I've seen several posts about validating dollar amounts with RegEx and I'm pretty close to what I need but with one issue. Here is what I have so far
^[\+\-]?\$?(\d*|\d{1,3}(,\d{3})*)(\.[0-9]+)?$
Here is what it accomplishes:
All these conditions are met but the issue I'm having is that if the user enters only a +, -, or $ then the expression returns true. What I'm looking for is if $+- exist they must be followed by something.
I know the issue is that a number before a decimal is optional, and the decimal section itself is also optional. But I want to allow entry of .25 w/o having to force it to be entered as 0.25.
I tried adding not end of line [^$] after the optional dollar sign \$? thinking that the +-$ couldn't be followed by end of line, but that didn't seem to work.
I appreciate any help!
-Shawn
You need to use the following regex
\d* is causing the problem, so use look ahead assertion. It confirms that there will be at least one character after the symbols., use \. instead else it will match any characterRegex :
^[+-]?\$?(?=.)(?:\d*|\d{1,3}(?:,\d{3})*)(?:\.[0-9]+)?$
Regex explanation here

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With