Dayana Benny Dayana Benny - 4 days ago 5
PHP Question

Show "invalid value" in html form (min=0 max=2) when decimal value given

I want to submit decimal values through html input box. But showing
input error as "invalid input" when giving value 1.5 where min=0 and
max=3.

html code is given below:

<input type="number" required value="<?php echo $someDecimalvalue; ?>" min="0" max="3" >

Answer

There is an attribute step for this purpose:

Works with the min and max attributes to limit the increments at which a numeric or date-time value can be set. It can be the string any or a positive floating point number. If this attribute is not set to any, the control accepts only values at multiples of the step value greater than the minimum.

So if you want to allow one decimal place, add step="0.1" to your input tag.

Comments