0

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.

4

3 回答 3

2

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

于 2013-03-13T14:56:10.750 回答
0

This regexp could help:

 ^\d+[,\.]?\d+$
于 2013-03-13T14:55:25.483 回答
0

/^\d+((\.|\,)\d)?$/

Matches 12, 12,0, 12.0. If you wanted to add many trailing digits, /^\d+(\.|\,)?\d+$/

于 2013-03-13T14:55:33.037 回答