1

I want one validation in JavaScript 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.

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.