Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex validation on decimal

Tags:

regex

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).

like image 406
BKK Avatar asked Jun 16 '26 08:06

BKK


1 Answers

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.

like image 181
nhahtdh Avatar answered Jun 17 '26 22:06

nhahtdh