Warm tip: This article is reproduced from stackoverflow.com, please click
javascript regex

Regex for pattern integer,integer or float

发布于 2020-03-29 12:47:45

I want to validate entry in textbox using regex. The pattern should be

integer, integer or float

so valid entries can be

1234, 1234
123, 123.45

and invalid entries will be

abd, 123
123, abc
abc, abc

so far I have tried

var entry = "123,123.4" 
var patt = new RegExp("^[0-9]+,(?:[\d+(\.\d+]+,?){0,1}$");
res = patt.test(entry);  

but it returns false, though it should return true

Questioner
Garima Jain
Viewed
70
Samvel Petrosov 2016-05-21 03:35

Replace you regExp by this one:

var reg = new RegExp("^[0-9]+,[0-9]+.?[0-9]*");

I think this is what you want.

To accept space after comma:

var reg = new RegExp("^[0-9]+, [0-9]+.?[0-9]*");

For case of 1a it's needed to ad $ in the end