Reputation: 213
I would like to validates format of a decimal, which need to be like : 0 or 0.5 or 1 or 1.5 ... Also, I must be able to accept "," or "." (for users of differents countries)
Could you help me please ? I'm not really good with regular expressions...
Thanks.
Upvotes: 0
Views: 1086
Reputation: 14740
/^\d+((\.|\,)\d)?$/
Matches 12
, 12,0
, 12.0
. If you wanted to add many trailing digits,
/^\d+(\.|\,)?\d+$/
Upvotes: 0
Reputation: 32787
You can use this regex
/^\d+([.,]\d+)?$/
^
is start of the string
$
is end of the string
^
,$
is essential else it would match anywhere in between..for example the above regex without ^,$ would also match xyz344.66xyz
\d
matches a single digit
+
is a quantifier that matches 1 to many preceding character or group..so \d+ means match 1 to many digits
?
means match preceding character or group optionally that is 0 to 1 time
Upvotes: 2