With the below HTML, when I type the value 3
in the input and click on submit
, Chrome (at least) shows the following error in a popup:
JavaScript
x
2
1
Please enter a valid value. The two nearest valid values are 2.0001 and 3.0001.
2
This doesn’t make sense. I’ve set the minimum value to be 0.0001 and I’m entering 3 which is greater than 0.0001.
JavaScript
1
3
1
document.getElementById('form').onsubmit = function(e) {
2
e.preventDefault()
3
}
JavaScript
1
12
12
1
<form id="form">
2
3
<input placeholder="0.000"
4
id="something"
5
type="number"
6
name="something"
7
min="0.0001"
8
required>
9
10
<button type="submit">submit</button>
11
12
</form>
Advertisement
Answer
The default value for the step
attribute is 1
, and since you’ve a min
applied on your input, there’s no integral n
such that min + n * step
(0.0001 + n * 1
) equals 3
.
You can change the step to 0.0001
or any
.
JavaScript
1
4
1
document.getElementById('form').onsubmit = function(e) {
2
e.preventDefault();
3
console.log(e.target.elements[0].value);
4
}
JavaScript
1
12
12
1
<form id="form" onsubmit="onSubmit">
2
<input
3
placeholder="0.000"
4
id="something"
5
type="number"
6
name="something"
7
min="0.0001"
8
step="any"
9
required
10
>
11
<button type="submit">submit</button>
12
</form>