Garima Jain Garima Jain - 2 years ago 80
Javascript Question

Regex for pattern integer,integer or float

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download