开发者

how to allow only floating point number to a text fiedl

How to allow only a valid floating point number into a text field floating point like these only

15

15.41

7045.12

0.74

--only 2 places after the digit

--max one dot

--only positive number开发者_JS百科 so no need for +/-


/^\d+(?:\.\d{1,2})?$/

and I bet this has been asked here before...

This regex allows integers or floats with one or two decimal places. The integer part is mandatory.

If your intent is not to validate input strings but to find decimal numbers inside a larger string, use this instead:

/\b\d+(?:\.\d{1,2})?\b/


Following should work

/\d+(\.\d{1,2})?/


This should work /([0-9]+(\.[0-9]{1,2})?)/ if you want to just match them. If you want to validate a string - /^([0-9]+(\.[0-9]{1,2})?)$/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜