max
max attribute
The max
attribute specifies the highest acceptable value for input types such as number, range, or date. It helps restrict user input and aids in form validation.
Purpose: The max
attribute establishes an upper limit for specific input types, improving form validation and overall user experience.
Syntax
<input max="number | datetime">
Syntax Examples
Input Type | Syntax | Example Code |
---|---|---|
Date | yyyy-mm-dd | <input type="date" min="2019-12-25" step="1"> |
Month | yyyy-mm | <input type="month" min="2019-12" step="12"> |
Week | yyyy-W## | <input type="week" min="2019-W23" step=""> |
Time | hh:mm | <input type="time" min="09:00" step="900"> |
Datetime-local | yyyy-mm-ddThh:mm | <input type="datetime-local" min="2019-12-25T19:30"> |
max
attribute with the min
attribute to define a valid range of values.Example
<label for="age">Enter your age (max 100):</label>
<input type="number" id="age" name="age" max="100">
max
and min
attributes are compatible with the following input types: number, range, date, datetime-local, month, time, and week.Values
- number
- The highest allowed numeric value in an input field.
- datetime
- The latest allowed datetime value in an input field.
Applies To
The max
attribute can be applied to the following HTML elements.
See also
Conclusion
The max
attribute in HTML sets an upper limit for specific input fields like number, date, and time, aiding in user input restriction and validation. It helps ensure that data entered by users falls within acceptable ranges. For better accuracy, combine it with the min attribute to create a valid range of values.
low
The HTML low attribute defines the minimum value for a <meter> element, indicating the lower limit of an acceptable range for measurements.
maxlength
The HTML maxlength attribute limits the number of characters a user can input in a text field, ensuring that the input remains within a specified length.