3

次の正規表現を使用して、ドット付きの 10 進数を検証しています.

/^[0-9]*\.?[0-9]*$/

ケースを除くすべてのケースで正常に機能します12.

作業例:

12
12.2
10.222
12.

ユーザーが入力したときに検証エラーをスローしたい ( 12.): 少なくとも小数点以下の数字を入力する必要があります ( など12.1)。

4

2 に答える 2

8

You can use this regex:

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

It will match whole number: 12, 1222

If there is a decimal point, then there must be at least 1 digit before and after the decimal point: 1.1, 34.2

These cases are not allowed: .43, 23.

于 2012-12-27T07:14:09.987 に答える
4

1 つまたは複数の量指定子を追加するだけです。

^[0-9]+(\.[0-9]+)?$
于 2012-12-27T07:16:13.707 に答える