问题
As the title of the question, I need to validate regex using the following values: (Maximum 2 decimal places, and 9 integers) with an optional percent symbol.
Valid:
10%
0%
1111111.12%
15.2%
10
2.3
Invalid:
.%
12.%
.02%
%
123456789123.123
I tryed:
^[0-9]{0,9}([\.][0-9]{0,2})\d[\%]{0,1}?$
But It does not work as I want.
回答1:
try this
^\d{1,9}(\.\d{1,2})?%?$
I tested over rubular and it is ok for your set of example.
回答2:
Try this:
^[0-9]{1,9}([\.][0-9]{1,2})?[\%]?$
来源:https://stackoverflow.com/questions/10560107/how-validate-decimal-with-optional-percentage-symbol-using-regex