4

my code is as follows:

<input type="number" min="4.50" max="9.90"  id="cpi" name="cpi" required="required" title="CPI" class="formfield3" />

CPI is a float value. But if I open it in browser and input value which is the float number such as 7.89 then it shows message of 'Invalid Value'. How to solve it?

0

2 Answers 2

11

Set step="any"

<input type="number" min="4.50" max="9.90" step="any" id="cpi" name="cpi" required="required" title="CPI" class="formfield3" />
Sign up to request clarification or add additional context in comments.

2 Comments

This is technically better than the approved answer, since the question does not specify any particular accuracy and can perhaps best be interpreted so that any number between the limits should be acceptable – and this is what step="any" does. But depending on the context, step="0.01" might correspond to what was intended or what is suitable.
When clicking the arrows to step up or down, it moves by 1. But now I can type a float without validation going crazy.
6

The number type has a step value controlling which numbers are valid (along with max and min), which defaults to 1. This value is also used by implementations for the stepper buttons (i.e. pressing up increases by step).

Simply change this value to whatever is appropriate. For money, two decimal places are probably expected:

<input type=number step=0.01 />

(I'd also set min=0 if it can only be positive)

As usual, I'll add a quick note: remember that client-side validation is just a convenience to the user. You must also validate on the server-side!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.