HTML maxlength Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Forms & Input

Introduction

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.

What You’ll Learn

01

Character cap

Count, not value.

02

text & textarea

Primary elements.

03

Positive int

e.g. maxlength="50".

04

vs max

Chars vs numbers.

05

minlength

Lower bound pair.

06

.maxLength

Update in JS.

Purpose of maxlength Attribute

The 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.

💡
Not the same as max

maxlength="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.

📝 Syntax

Add maxlength with a non-negative integer to text-like inputs or textarea:

maxlength.html
<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">

Syntax Rules

  • Value is a non-negative integer (character count), e.g. maxlength="50".
  • Valid on input types: text, search, url, tel, email, password.
  • Valid on <textarea> elements.
  • Not valid on number, date, checkbox, radio, etc.—use max for those.
  • Pair with minlength for a minimum character requirement.
  • JavaScript IDL property: element.maxLength = 100 (camelCase, number).

💎 Values

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).
  • Attribute absent — No HTML-enforced character limit (browser default).
maxlength-js.html
document.getElementById("dynamicField").maxLength = 100;
document.getElementById("comment").setAttribute("maxlength", "500");

⚡ Quick Reference

Use caseMarkupNotes
Username<input maxlength="20">Short text cap
Comment box<textarea maxlength="500">Multiline limit
Password fieldtype="password" maxlength="128"Prevent huge payloads
Number capUse max, not maxlengthDifferent attribute
Min + max lengthminlength="8" maxlength="64"Password rules
JS updateel.maxLength = 50Number property

Applicable Elements

Element / typeSupported?Notes
<input type="text">YesMost common use
<textarea>YesMultiline character limit
email, url, tel, search, passwordYesText-like input types
<input type="number">NoUse max for value limit
date, time, checkbox, radioNoNot a character-count attribute

maxlength vs max vs minlength

AttributeApplies toLimits
maxlengthtext, email, password, textarea, etc.Maximum character count
minlengthSame text-like inputs and textareaMinimum character count (validation)
maxnumber, range, date, time, meter, progressMaximum numeric or date/time value

Examples Gallery

Text input with live counter, form with textarea limit, and dynamic maxLength with JavaScript.

👀 Live Preview

Bio field with maxlength="50" and character counter:

0 / 50 characters

Example — Text Input Character Limit

Cap a single-line field at 50 characters:

text-maxlength.html
<label for="bio">Short bio:</label>
<input type="text" id="bio" maxlength="50">
Try It Yourself

How It Works

The browser counts UTF-16 code units as the user types. Once the count reaches maxlength, additional keystrokes are ignored (paste may truncate).

Example — Form with Input and Textarea

Apply different limits to username and message fields:

form-maxlength.html
<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>
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

Change the character limit when rules change at runtime:

dynamic-maxlength.html
<input type="text" id="dynamicField" maxlength="10">

<script>
  document.getElementById("dynamicField").maxLength = 25;
</script>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Announce limits in labels or hints — e.g. “Bio (max 50 characters)” so screen reader users know the cap before typing.
  • Provide live character counts — Use visible text updated on input events; consider aria-live="polite" for dynamic counters.
  • Do not rely on maxlength alone — Assistive technology users still need server-validated limits.
  • Avoid overly tight limits — Restrictive caps frustrate users and may block valid names or messages.
  • Pair with clear error messages — When using minlength, explain requirements in plain language.

🧠 How maxlength Works

1

Author sets maxlength

Integer character cap.

Markup
2

User types text

Browser counts chars.

Input
3

Limit reached

Further input blocked.

Enforce
=

Controlled text length

Data fits expected size.

Browser Support

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.

HTML5 · Fully supported

Universal character limits

All major browsers enforce maxlength on text inputs and textarea.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
maxlength on text inputs & textarea 99% supported

Bottom line: Safe to use on supported elements; validate length on the server too.

💡 Best Practices

✅ Do

  • Set limits that match database column sizes
  • Show remaining characters with a live counter
  • Pair with minlength when minimum length matters
  • Validate string length again on the server
  • Use realistic values (e.g. 20+ for usernames)

❌ Don’t

  • Use maxlength on type="number" (use max)
  • Confuse maxlength with max
  • Set limits so low users cannot complete the form
  • Trust client maxlength alone for security
  • Forget to document limits in labels or help text

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about maxlength

Bookmark these before building text forms.

5
Core concepts
📄 02

text & textarea

Supported elements.

Scope
📈 03

vs max

Chars vs values.

Compare
⚙️ 04

.maxLength

JS number property.

Dynamic
🔒 05

Server too

Client ≠ secure.

Security

❓ Frequently Asked Questions

It sets the maximum number of characters a user can enter in a text-like input or textarea. The browser prevents typing beyond that limit.
No. maxlength 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.
Use 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.
No. Client-side maxlength can be bypassed. Always validate string length on the server before storing data.

Limit text input with maxlength

Practice character caps on text inputs, textarea, and dynamic maxLength in the Try It editor.

Try text input example →

About the author

Mari Selvan M P
Mari Selvan M P πŸ”—

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful