HTML maxlength Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

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.

Attribute Values

One value type — a non-negative integer representing the maximum length:

ValueDescriptionTry it
numberInteger ≥ 0. Caps how many UTF-16 code units the user can enter (e.g. 20, 160)Try it
0Allows no characters. Rarely useful for real formsTry it
Omitted / invalidNo maximum length is enforced by HTMLTry it
With minlengthmaxlength must be ≥ minlength when both are validTry it

Live Preview

Change the maxlength value, then type in the field. The counter updates as you type.

A real text input. Apply a limit, then try typing past it.

Text input

0 / 20

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Text Input Character Limit

Cap a single-line field so users cannot type past a fixed length.

HTML
<label for="username">Username (max 20):</label>
<input type="text" id="username" name="username" maxlength="20">
Try It Yourself

How It Works

  1. Add the attribute. maxlength="20" sets an upper character cap.
  2. Use a text-like type. Works on text, email, password, and similar types.
  3. Label the control. for / id keeps the field accessible.
  4. Browser enforces entry. Extra keystrokes are ignored; still validate on the server.

Example 2 — Textarea Character Limit

Limit multiline comments or bios the same way as single-line inputs.

HTML
<label for="bio">Bio (max 160):</label>
<textarea id="bio" name="bio" maxlength="160" rows="4"></textarea>
Try It Yourself

How It Works

  1. Same attribute name. maxlength on <textarea> works like on <input>.
  2. Every character counts. Letters, spaces, and most line breaks use the limit.
  3. rows is layout only. Visible height does not change the character cap.
  4. Paste may truncate. Pasted text is often cut to fit maxlength.

Example 3 — minlength + maxlength Together

Define an allowed length range for a product name and description.

HTML
<label for="name">Product name:</label>
<input
  id="name"
  name="name"
  type="text"
  minlength="3"
  maxlength="20"
  required>

<label for="description">Product description:</label>
<textarea
  id="description"
  name="description"
  minlength="10"
  maxlength="40"
  required></textarea>
Try It Yourself

How It Works

  1. minlength requires enough characters before the field is valid.
  2. maxlength stops the value from growing past the upper cap.
  3. Order the bounds. maxlength must be ≥ minlength.
  4. 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.

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 Chrome 4+
Full support
Mozilla Firefox 4+
Full support
Apple Safari 5.1+
Full support
Microsoft Edge 12+
Full support
Opera 15+
Full support
Samsung Internet 4+
Full support
Internet Explorer 10+
Supported
maxlength Widely supported

Bottom line: Safe to use everywhere for character caps. Treat client limits as UX, and enforce the same rules on the server.

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").

Next: media

Control when linked stylesheets and other resources apply.

media tutorial →

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.

11 people found this page helpful