How can i validate following logic from regex
- Allow Dollar($),Comma(,),Decimal(.) and digit(0-9)
- Can have only 8 digit before Decimal irrespective of $ and ma
- 2 digit after decimal
Allowed string Example:
- 99999999.99
- $99999999.99
- $9,999,9999.99
- $9999,99,99.99
Does not allow : - $999999999.99 ( 9 digit before decimal) - $99,99,999,99.99
Mean i want to restrict the count of digit only before decimal. How can i achieve this. Thanks in Advance
How can i validate following logic from regex
- Allow Dollar($),Comma(,),Decimal(.) and digit(0-9)
- Can have only 8 digit before Decimal irrespective of $ and ma
- 2 digit after decimal
Allowed string Example:
- 99999999.99
- $99999999.99
- $9,999,9999.99
- $9999,99,99.99
Does not allow : - $999999999.99 ( 9 digit before decimal) - $99,99,999,99.99
Mean i want to restrict the count of digit only before decimal. How can i achieve this. Thanks in Advance
Share Improve this question asked Mar 3, 2015 at 10:47 MayankMayank 1,3925 gold badges25 silver badges45 bronze badges 2- Does it have to be done entirely in regexp? – Barmar Commented Mar 3, 2015 at 10:50
- ^(\$?(\,?\d){1,8}\.\d{2})$ – Vladu Ionut Commented Mar 3, 2015 at 11:06
2 Answers
Reset to default 4You can use this regex:
/^\$?(?:,?\d){1,8}(?:\.\d{1,2})?$/gm
RegEx Demo
Explanation:
^ # Line start
\$? # match optional $ at start
(?:,?\d) # Match an optional ma followed by a digit and use non-capturing group
{1,8} # up to 8 occurrence of previous group
(?:\.\d{1,2})? # followed by optional decimal point and 1 or 2 digits
$ # line end
/^(\$?(\,?\d){1,8}\.\d{2}$)/gm
Regex Demo