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
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
You should escape your dot
What are you trying to do with the '?+' at the end? Did you mean to put a '*'?
This works, only can you update it to even accept space please. So if its 123, 123.4 (with a space) it should also pass. Thanks in advance
If you need it to accept spaces you should probably add a couple of \s+. But it would make sense to do what Dhaval said and use String.split(',') and then String.trim() each string.
This will match on
1, 1a
- which isn't really valid.