Numeric Constraints: min, max, and step
What You'll Learn
You'll learn how to control the valid range and intervals for number and date inputs using HTML constraint attributes.
The Core Idea
When users enter numbers or dates, you often need to enforce boundaries—like "quantity must be at least 1" or "choose a date in the future." The min, max, and step attributes let you declare these rules directly in your HTML, and the browser will enforce them automatically during validation.
Think of it like setting rules on a thermostat: the minimum temperature (min), maximum temperature (max), and what increments you can adjust by (step).
How They Work
The min Attribute
Sets the lowest acceptable value:
<input type="number" name="age" min="18" required>
This ensures users can't submit a value below 18.
The max Attribute
Sets the highest acceptable value:
<input type="number" name="quantity" max="100" required>
Prevents submissions above 100.
The step Attribute
Defines the legal intervals. For example, step="5" means only multiples of 5 are valid:
<input type="number" name="donation" min="10" max="500" step="5">
Valid values: 10, 15, 20, etc. Invalid: 11, 13, 17.
Combining All Three
<input type="number" name="score" min="0" max="100" step="0.5" required>
This accepts scores from 0 to 100 in half-point increments (0, 0.5, 1.0, etc.).
Works with Date Inputs Too
<input type="date" name="appointment" min="2024-01-01" max="2024-12-31">
Key Takeaway: Use min, max, and step attributes to enforce numeric ranges and valid intervals, letting the browser validate input boundaries automatically before form submission.