Regex for number with decimals and thousand separator
I need regex to validate a number that could contain thousand separators or decimals using javascript. Max value being 9,999,999.99
Min value 0.01
Other valid values: 11,111
11.1
1,111.11
INVALID values: 1111
1111,11
,111
111,
I've searched all over with no joy.
/^d{1,3}(,d{3})*(.d+)?$/
About the minimum and maximum values... Well, I wouldn't do it with a regex, but you can add lookaheads at the beginning:
/^(?!0+.00)(?=.{1,9}(.|$))d{1,3}(,d{3})*(.d+)?$/
Note: this allows 0,999.00
, so you may want to change it to:
/^(?!0+.00)(?=.{1,9}(.|$))(?!0(?!.))d{1,3}(,d{3})*(.d+)?$/
which would not allow a leading 0.
Edit:
Tests: http://jsfiddle.net/pKsYq/2/
((d){1,3})+([,][d]{3})*([.](d)*)?
It worked on a few, but I'm still learning regex as well.
The logic should be 1-3 digits 0-1 times, 1 comma followed by 3 digits any number of times, and a single . followed by any number of digits 0-1 times
I have used below regrex for following retrictions -
^(?!0|.00)[0-9]+(,d{3})*(.[0-9]{0,2})$
上一篇: 如何在Java中为正则表达式转义文本
下一篇: 用小数点和千位分隔符的正则表达式