👀 Live Preview
Bio field with maxlength="50" and character counter:
0 / 50 characters

The maxlength attribute limits how many characters a user can type into a text-like <input> or <textarea>. Unlike max (which caps numeric or date/time values), maxlength counts letters, digits, spaces, and symbols. The browser blocks further typing once the limit is reached. Use it for usernames, bios, comments, and any field where database columns or UX design require a fixed upper length.
Count, not value.
Primary elements.
e.g. maxlength="50".
Chars vs numbers.
Lower bound pair.
Update in JS.
maxlength AttributeThe primary purpose of maxlength is to enforce an upper bound on text length at the point of entry. It protects database column sizes, keeps UI layouts predictable, and gives users immediate feedback before they over-type. For example, a tweet-style field might use maxlength="280"; a postal code field might use maxlength="10".
Browsers enforce maxlength by preventing additional keystrokes (and most paste operations truncate to the limit). This is softer UX than submitting an error after the fact. Still complement it with server-side length checks—client limits can be bypassed.
maxmaxlength="50" allows fifty characters in a text field. max="50" on type="number" allows any string length but validates that the numeric value is at most 50. Use the right attribute for the input type.
Add maxlength with a non-negative integer to text-like inputs or textarea:
<label for="username">Username:</label>
<input type="text" id="username" maxlength="20">
<label for="bio">Bio:</label>
<textarea id="bio" maxlength="160"></textarea>
<label for="email">Email:</label>
<input type="email" id="email" maxlength="254">maxlength="50".input types: text, search, url, tel, email, password.<textarea> elements.number, date, checkbox, radio, etc.—use max for those.minlength for a minimum character requirement.element.maxLength = 100 (camelCase, number).The maxlength attribute accepts a non-negative integer:
maxlength="20" — Up to 20 characters in a username field.maxlength="280" — Short post or status message limit.maxlength="5000" — Longer textarea content with a generous cap.maxlength="0" — Effectively blocks all input (rare; avoid confusing users).document.getElementById("dynamicField").maxLength = 100;
document.getElementById("comment").setAttribute("maxlength", "500");| Use case | Markup | Notes |
|---|---|---|
| Username | <input maxlength="20"> | Short text cap |
| Comment box | <textarea maxlength="500"> | Multiline limit |
| Password field | type="password" maxlength="128" | Prevent huge payloads |
| Number cap | Use max, not maxlength | Different attribute |
| Min + max length | minlength="8" maxlength="64" | Password rules |
| JS update | el.maxLength = 50 | Number property |
| Element / type | Supported? | Notes |
|---|---|---|
<input type="text"> | Yes | Most common use |
<textarea> | Yes | Multiline character limit |
email, url, tel, search, password | Yes | Text-like input types |
<input type="number"> | No | Use max for value limit |
date, time, checkbox, radio | No | Not a character-count attribute |
maxlength vs max vs minlength| Attribute | Applies to | Limits |
|---|---|---|
maxlength | text, email, password, textarea, etc. | Maximum character count |
minlength | Same text-like inputs and textarea | Minimum character count (validation) |
max | number, range, date, time, meter, progress | Maximum numeric or date/time value |
Text input with live counter, form with textarea limit, and dynamic maxLength with JavaScript.
Bio field with maxlength="50" and character counter:
0 / 50 characters
Cap a single-line field at 50 characters:
<label for="bio">Short bio:</label>
<input type="text" id="bio" maxlength="50">The browser counts UTF-16 code units as the user types. Once the count reaches maxlength, additional keystrokes are ignored (paste may truncate).
Apply different limits to username and message fields:
<form>
<label for="username">Username:</label>
<input type="text" id="username" maxlength="20">
<label for="message">Message:</label>
<textarea id="message" maxlength="100"></textarea>
<button type="submit">Submit</button>
</form>Each control enforces its own limit. Use realistic values—the old reference used maxlength="5" for usernames, which is usually too restrictive for real apps.
Change the character limit when rules change at runtime:
<input type="text" id="dynamicField" maxlength="10">
<script>
document.getElementById("dynamicField").maxLength = 25;
</script>The IDL property maxLength (camelCase, number) mirrors the content attribute. The old reference incorrectly said the script set 10 but described 100—always match code and explanation.
input events; consider aria-live="polite" for dynamic counters.minlength, explain requirements in plain language.Integer character cap.
Browser counts chars.
Further input blocked.
Data fits expected size.
The maxlength attribute is fully supported on all listed text-like inputs and <textarea> in modern browsers. Enforcement behavior is consistent across Chrome, Firefox, Safari, and Edge.
All major browsers enforce maxlength on text inputs and textarea.
Bottom line: Safe to use on supported elements; validate length on the server too.
minlength when minimum length mattersmaxlength on type="number" (use max)maxlength with maxThe maxlength attribute is a practical way to cap how much text users can enter in form fields. Applied to text-like inputs and textarea elements, it improves data quality and user experience by stopping over-length input before submission.
Choose sensible limits, show character counts where helpful, distinguish maxlength from max, and always enforce length on the server. Used thoughtfully, it keeps forms clean and databases safe.
maxlengthBookmark these before building text forms.
Not numeric max.
MeaningSupported elements.
ScopeChars vs values.
CompareJS number property.
DynamicClient ≠ secure.
Securitymaxlength limits character count on text fields. max limits numeric or date/time values on number, range, date, and similar inputs.input types text, search, url, tel, email, and password, plus <textarea>. Not number, date, checkbox, or radio.element.maxLength = 100 (camelCase number) or setAttribute("maxlength", "100").minlength sets the minimum required character count for validity. Use it with maxlength to define an acceptable text length range.Practice character caps on text inputs, textarea, and dynamic maxLength in the Try It editor.
5 people found this page helpful