👀 Live Preview
Quantity field with min="1" and max="100":
Values above 100 fail browser constraint validation when the form is submitted or checkValidity() runs.

The max attribute sets the maximum allowed value on certain <input> types and defines the upper bound of the scale on <meter> and <progress> elements. It is essential for form validation—capping quantities, limiting date pickers, and constraining range sliders. Pair it with min to define a valid range. Do not confuse max with maxlength (character count on text inputs) or high on <meter> (warning threshold, not the scale limit).
Cap user input.
Numeric max value.
ISO-style strings.
Valid range pair.
Value vs characters.
Update in JS.
max AttributeThe primary purpose of the max attribute is to define the highest value a user may enter or select. On a number field, it prevents quantities above a business rule (e.g. stock on hand). On a date field, it blocks dates after a deadline. On a range slider, it sets the right end of the track.
Browsers use max (with min and step) for constraint validation. When the value exceeds max, the input is invalid and :invalid styling or checkValidity() reflects the error. This improves UX but must be repeated on the server for security.
maxlength or meter highmaxlength limits how many characters can be typed in text inputs. high on <meter> marks a warning zone on a gauge—it does not cap user input. max on <input type="number"> sets the numeric upper bound for form validation.
Add max to supported input types (often with min):
<label for="qty">Quantity:</label>
<input type="number" id="qty" min="1" max="100">
<label for="eventDate">Event date:</label>
<input type="date" id="eventDate" max="2025-12-31">
<label for="closing">Closing time:</label>
<input type="time" id="closing" max="18:00">
<input type="range" min="0" max="500" value="250">type: number/range use numeric strings; date uses YYYY-MM-DD; time uses HH:MM or HH:MM:SS.datetime-local uses YYYY-MM-DDTHH:MM (e.g. 2025-12-31T23:59).min ≤ max when both are set.<meter> and <progress>, max defines the scale upper bound (default often 1 or 100).type="text", checkbox, radio, etc.—use maxlength for text length.input.max = "100" (string) on HTMLInputElement.The max attribute accepts a string whose format matches the control type:
max="100", max="3.5" (valid floating-point string).max="2025-12-31" (YYYY-MM-DD).max="18:00" or max="18:00:00".max="2025-12-31T23:59".max="2025-12"; Week — max="2025-W52".max="100" sets the top of the scale (meter also uses high for warning zones).document.getElementById("ticketPrice").max = "500";
document.getElementById("dynamicMaxField").setAttribute("max", "1000");| Input type | Example max | Format |
|---|---|---|
number | max="100" | Numeric string |
range | max="500" | Slider upper bound |
date | max="2025-12-31" | YYYY-MM-DD |
time | max="18:00" | HH:MM |
datetime-local | max="2025-12-31T23:59" | ISO local datetime |
text | Use maxlength | Not max |
| Element / type | Supported? | Notes |
|---|---|---|
<input type="number"> | Yes | Primary use case |
<input type="range"> | Yes | Slider maximum |
<input type="date">, time, datetime-local | Yes | Latest selectable value |
<input type="month">, week | Yes | Type-specific string format |
<meter>, <progress> | Yes | Scale upper bound |
<input type="text"> | No | Use maxlength instead |
max vs min vs maxlength vs meter high| Attribute | Applies to | Purpose |
|---|---|---|
max | number, range, date/time inputs; meter, progress | Maximum allowed value or scale bound |
min | Same input types; meter, progress | Minimum allowed value or scale bound |
maxlength | text, search, url, tel, email, password | Maximum character count |
high | <meter> only | Warning threshold on gauge (not form limit) |
Number input cap, date deadline, and dynamic max on a range slider with JavaScript.
Quantity field with min="1" and max="100":
Values above 100 fail browser constraint validation when the form is submitted or checkValidity() runs.
Cap ticket quantity or price with max on a number field:
<label for="ticketPrice">Ticket price ($):</label>
<input
type="number"
id="ticketPrice"
name="ticketPrice"
min="0"
max="500"
step="0.01">The browser compares the current value to max during constraint validation. Invalid values block form submission unless JavaScript bypasses checks—another reason to validate on the server.
Combine date and number limits in one form:
<form>
<label for="eventDate">Event date:</label>
<input type="date" id="eventDate" max="2025-12-31">
<label for="ticketPrice">Ticket price:</label>
<input type="number" id="ticketPrice" max="500">
<button type="submit">Submit</button>
</form>Each input type interprets max in its own format. The event date cannot exceed the end of 2025; ticket price cannot exceed 500. Both use the same attribute name with type-specific values.
Adjust the maximum at runtime when business rules change:
<input type="range" id="price" min="0" max="500" value="250">
<script>
document.getElementById("price").max = "1000";
</script>Assigning a string to input.max updates the upper bound immediately. The range slider thumb can move further right after the limit increases. Re-validate the current value if the new max is lower than what the user already entered.
<label for="id"> so users know what the max limit applies to.aria-invalid when you enhance validation with JavaScript.max, consider an aria-live region explaining the new limit.max alone is insufficient.Upper bound in markup.
Type, pick, or slide.
value ≤ max required.
Or :invalid if over max.
The max attribute is fully supported on all listed input types, <meter>, and <progress> in modern browsers. Validation behavior is consistent across Chrome, Firefox, Safari, and Edge.
All major browsers honor max on number, range, and date/time inputs.
Bottom line: Safe to use on supported input types; always validate on the server too.
max with min for clear valid rangesmax in JS when dynamic limits applymax on text inputs (use maxlength)max with meter high warning thresholdsmax alone for securitymin greater than maxstep when fractional numbers matterThe max attribute is a valuable tool for defining upper limits on user input in HTML forms. Whether you are capping quantities, limiting date pickers, or setting range slider bounds, max works with browser constraint validation to guide users toward valid data.
Remember to pair it with min, distinguish it from maxlength, and always enforce limits on the server. Used thoughtfully, max improves accuracy and the overall form experience.
maxBookmark these before building your next form.
Cap allowed values.
MeaningType-specific format.
SyntaxValue vs chars.
CompareDefine valid range.
PairClient max ≠ secure.
Security<input> types and the upper bound of the scale on <meter> and <progress>. Values above max fail constraint validation.max limits numeric or date/time values on specific input types. maxlength limits how many characters can be typed in text-like inputs.number, range, date, time, datetime-local, month, and week. Text inputs use maxlength instead.inputElement.max = "100" or setAttribute("max", "100"). The value is a string matching the input type’s expected format.min="1" max="100" on a quantity field. Ensure min ≤ max.max is client-side constraint validation only. Attackers can bypass it. Always enforce upper limits on the server.Practice the max attribute on number, date, and range inputs in the Try It editor.
5 people found this page helpful