Reputation: 33
I have regex which should check if string is decimal number...
^([\+\-])?\d{1,10}[\.,]?\d{1,10}?$
but is not working in case of
-1 //single digit
and if there are more than 10 digits w/o decimal separator -> should fail
assertEquals(false, Util.isDecimalNumber("12345678901"));
Is there a easier way to check if string is a decimal number?
Upvotes: 1
Views: 77
Reputation: 1084
^[\+\-]?\d{1,10}([\.,]\d{1,10})?$
Fiddle: https://regex101.com/r/qQ2dE4/80
Upvotes: 0
Reputation: 34199
You have an incorrect syntax in the fractional part of your RegExp.
Instead of
^([\+\-])?\d{1,10}[\.,]?\d{1,10}?$
It should be either
^([\+\-])?\d{1,10}([\.,]\d{1,10})?$
or
^([\+\-])?\d{1,10}[\.,]?\d{0,10}$
The first one is more suitable, because it disallows decimal point without fractional part like 123.
.
In words, it will become:
start with single + or - //not mandatory.
follow with digit
follow with decimal single separator and follow with the one to ten digits //not mandatory
Check this Regex101 demo out.
Upvotes: 1