max

The HTML max attribute sets the upper limit for an input element, such as a number or date, to restrict user input.

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

index.html
<input max="number | datetime">

Syntax Examples

Input TypeSyntaxExample Code
Dateyyyy-mm-dd<input type="date" min="2019-12-25" step="1">
Monthyyyy-mm<input type="month" min="2019-12" step="12">
Weekyyyy-W##<input type="week" min="2019-W23" step="">
Timehh:mm<input type="time" min="09:00" step="900">
Datetime-localyyyy-mm-ddThh:mm<input type="datetime-local" min="2019-12-25T19:30">
Combine the max attribute with the min attribute to define a valid range of values.

Example

index.html
<label for="age">Enter your age (max 100):</label>
<input type="number" id="age" name="age" max="100">
The 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.