The maxlength attribute sets the maximum number of characters a user can enter in a text-like <input> or a <textarea>. The value must be an integer of 0 or higher. Length is measured in UTF-16 code units (often the same as character count, but not always — for example some emoji use two units).
Supported input types: text, search, url, tel, email, and password. Pair with minlength for a lower bound, and use max instead when you need a numeric or date/time ceiling.
Baseline · Widely available.
Not deprecated and not experimental. Stable across modern browsers since about July 2015.
Client hint — not server security.
Browsers usually block typing past the limit, but values can still be changed outside the UI. Always check length on the server.
Syntax
HTML Syntax
syntax.html
<input type="text" maxlength="number">
<textarea maxlength="number"></textarea>
number is a non-negative integer. If the attribute is missing or invalid, there is no maximum length.
Values
Attribute Values
One value type — a non-negative integer representing the maximum length:
Value
Description
Try it
number
Integer ≥ 0. Caps how many UTF-16 code units the user can enter (e.g. 20, 160)
Too short fails validation; too long is blocked by maxlength.
How It Works
minlength requires enough characters before the field is valid.
maxlength stops the value from growing past the upper cap.
Order the bounds.maxlength must be ≥ minlength.
required blocks empty submit; length rules apply once the user edits the value.
Edge Cases & Pitfalls
Most issues come from mixing up attributes or trusting the browser alone.
compare
Do not confuse with max
maxlength counts characters. max limits numeric or date/time values on types like number and date.
counting
UTF-16 code units, not always “letters”
Some emoji and rare characters use two UTF-16 units, so visible glyphs may not match value.length one-to-one.
validation
tooLong if the value is somehow longer
If length exceeds maxlength, constraint validation fails and ValidityState.tooLong is true. Checks apply after the user changes the value.
bounds
Keep maxlength ≥ minlength
An invalid range makes the length constraints unreliable. Set a clear floor and ceiling.
security
Always re-check on the server
HTML limits are UX helpers. Attackers can submit longer strings without using your form UI.
Compatibility
Browser Support
maxlength is an HTML attribute for text inputs and textarea in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
maxlength
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support it on text-like inputs and textarea.
97%Global support (approx.)
Google Chrome4+
Full support
Mozilla Firefox4+
Full support
Apple Safari5.1+
Full support
Microsoft Edge12+
Full support
Opera15+
Full support
Samsung Internet4+
Full support
Internet Explorer10+
Supported
maxlengthWidely supported
Bottom line: Safe to use everywhere for character caps. Treat client limits as UX, and enforce the same rules on the server.
Remember
Key Takeaways
Role: cap how many UTF-16 code units a user can enter.
Elements: text-like <input> types and <textarea>.
Pair with minlength: define a clear length range when needed.
Not max: use max for number/date values, not character count.
One line:maxlength stops over-typing in the browser — it does not replace server-side length checks.
Frequently Asked Questions
It sets the maximum length of text a user can enter in a text-like input or a textarea. The length is measured in UTF-16 code units. Browsers usually stop further typing once the limit is reached.
input types text, search, url, tel, email, and password, plus textarea. It does not apply to number, date, checkbox, or radio — use max for numeric or date/time limits.
No. maxlength caps character count on text fields. max caps numeric or date/time values on number, range, date, and similar types.
minlength is the minimum required length; maxlength is the upper cap. If both are set, maxlength must be greater than or equal to minlength.
No. It is client-side only and can be bypassed. Always validate string length on the server before storing or processing data.
Use the camelCase property element.maxLength = 100 on an input or textarea, or call setAttribute("maxlength", "100").