In this price validation i have to make sure only 3 digits should be there after decimal not two or one. For this i have used this expression to verify 3 digits after decimal but it's also working when there is only 2 digits. i'm using regex for this. I'm validating this value -1,564,736.130 .
\.[0-9]{3}
CodePudding user response:
The regex you wrote is working fine, It will match numbers which have 3 digits after the decimal but it will also match numbers with more than 3 digits after the decimal.
So I suggest that you add '$' to the end of the regex so that it doesn't match anything that has more than 3 digits after the decimal.
\.[0-9]{3}$
CodePudding user response:
You may match the whole number, or (at least) the numeric part, but considering just the fractional part ; you have to add a suffix:
\.[0-9]{3}([^0-9,.]|$)
For the whole number, add the start pattern:
(^|[^0-9,.])[0-9,]
The whole expression:
(^|[^0-9,.])[0-9,] \.[0-9]{3}([^0-9,.]|$)
You now need to check the thousand separator and the grouping (per 3 digits):
(^|[^0-9,.])[0-9,]{1,3}(,[0-9]{3})*\.[0-9]{3}([^0-9,.]|$)