HTML Number and Range Inputs: Values and Steps
Enter an exact number
Use type="number" for quantities, not identifiers such as postal codes. min and max constrain acceptable values; step sets the allowed increment. The initial value is 3. A number field can be empty unless required.
<label> Tickets <input type="number" min="1" max="8" step="1" value="3"></label>The field starts at 3 and accepts whole quantities from 1 to 8.
Use a slider for approximate choices
A range input is useful when the exact number is less important than a position on a scale. Set explicit limits and an initial value. It cannot represent an empty value; the browser normalizes it to a permissible value.
<label> Volume <input type="range" min="0" max="100" step="10" value="40"></label>A slider starts at 40, with steps of 10.
Decimal values and the step base
The step grid normally starts at min when supplied. For min=0.25 and step=0.5, 0.25, 0.75 and 1.25 are valid; 1 is not. step="any" removes the step constraint, not the range constraint. Browser controls do not replace server-side checks.
<label> Weight (kg) <input type="number" min="0.25" max="5" step="0.5" value="0.75"></label>0.75 fits the allowed grid. Try typing 1 to see a step mismatch on form validation.
Common mistakes and practice
Use text for account codes and telephone numbers, even when they contain digits. A number field may accept a temporary invalid entry while editing. Read .value as text or .valueAsNumber as a number (NaN for an empty field). The JavaScript properties are previewed here; their API is documented separately. Practice choosing a control for quantity versus brightness.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Number and Range Inputs: Values and Steps — Knowledge Check
Apply the control concepts taught in this lesson.
Which control is best for an exact ticket count?
With min=2 and step=3, which value is valid?
What does step="any" remove?
Why use a text input for a postal code rather than a number input?
What happens to an optional number input when the user clears it?