开发者

JavaScript validation for numeric values

I want one validation in JavaScrip开发者_运维知识库t so that a text boxt should allow to enter only numeric values in the following format:

4.25
4.50
2.00

and so on. There should be always 2 digits after the decimal point and 1 digit before the decimal point. If the user enters 2, it should display as 2.00. This is my requirement.


For having only 2 digits after . you can use Number().toFixed(2) function in javscript

var num = Number(4).toFixed(2) will give you 4.00

var num = Number(4.1).toFixed(2) will give you 4.10

if you do not have number in place after ".", then toFixed will append 0 to your number.

For you second question, probably you can split your number on "." and check the first element is of length 1.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜