BKK
BKK

Reputation: 1199

Regex validation on decimal

I am using the following regex to validate decimal numbers with dot .

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

It works fine for all the cases except the case 12.

Working Example:

12
12.2
10.222
12.

I want to throw validation error when user enters (12.): at least a digit after decimal point needs to be entered (like 12.1).

Upvotes: 3

Views: 3857

Answers (2)

vmeln
vmeln

Reputation: 1309

Simply add one or more quantificator:

^[0-9]+(\.[0-9]+)?$

Upvotes: 4

nhahtdh
nhahtdh

Reputation: 56809

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.

Upvotes: 8

Related Questions