In this article, we are going to learn how to set a minimum and maximum value for an input element by using the HTML <input> min and max attribute. This prevents the input element from accepting values that are lower than the minimum value or higher than the maximum.
Approach: This can be implemented by using the min and max attributes:
- min: This attribute accepts a minimum value for the input element.
- max: This attribute accepts a minimum value for the input element.
These parameters together can be used to specify a range of numbers that can be accepted as input. Note that these attributes only work on the input type of number and date.
<input type="number" min="min_value" max="max_value">
Example: In this example, we have used two inputs with different input types, one is a number type and the other is the date type. The minimum and maximum range of the number is from 1 to 100 and the dates are from 01-01-2020 to 01-01-2021.