Textarea Character Limit: maxlength and a Live Counter

November 11, 2026 ยท 9 min read

Textarea Character Limit: maxlength, a Live Counter and Emoji

To set a textarea character limit, add maxlength="280" to the <textarea> tag and the browser stops accepting input at 280. That one attribute covers typing and paste. It counts UTF-16 code units, though, so this tutorial adds a live counter that agrees with it and a server check for what slips past.

How to Set a Textarea Character Limit With maxlength

The maxlength attribute takes a non-negative integer. Pair it with minlength when you also want a floor, and with required when an empty box shouldn't pass.

<label for="bio">Short bio</label>
<textarea id="bio" name="bio" rows="4"
          minlength="20" maxlength="280" required></textarea>

Once the value reaches 280 code units, further keystrokes are ignored. A value under 20 units fails validation when the form is submitted, and the browser shows its own message. Per MDN's textarea reference, an empty box with minlength still counts as valid unless required is set too.

There's no default cap. Without maxlength, the user can type or paste as much as they like. Any limit you hit in practice comes from your server, your database column or your framework. If your field feeds a post or a text message, our tweet character counter reference lists the limits for X, Instagram and SMS.

The same two attributes work on a single-line field. A text field character limit is <input type="text" maxlength="60">, and it follows exactly the rules below, minus the line-break quirks.

What Counts Toward maxlength: UTF-16 Code Units, Not Characters

The HTML spec measures the limit with the string's length, which means UTF-16 code units. Most Latin letters take one unit. Many emoji take two, and some take four or more. The user sees one symbol each time, but the attribute spends more of the budget.

Here's how many of each symbol fit in a box with maxlength="10". We measured the unit counts in Node.js 22:

Typed symbolUTF-16 units (what maxlength counts)Symbols that fit in maxlength 10
a110
รฉ (one precomposed code point)110
e plus a combining accent25
๐Ÿ˜€25
๐Ÿ‡ซ๐Ÿ‡ท (flag)42
๐Ÿ‘ฉโ€๐Ÿ’ป (woman technologist)52

So a 280-unit bio field holds 280 plain letters but only 140 smiley faces. There's no attribute that counts what users perceive as characters instead. That's a real limitation of maxlength, and the only way around it is JavaScript.

If you need the perceived count, Intl.Segmenter with granularity: "grapheme" splits text into user-visible characters. MDN lists it as Baseline widely available, supported across browsers since April 2024. Our character counter guide compares code units, code points and graphemes side by side.

How Do Line Breaks Count in a Textarea Character Count?

Inside the browser, each line break counts as one unit. The WHATWG maxlength definition says textarea newlines are normalized to a single character before the check, not counted as CRLF pairs. The value property you read in JavaScript uses that same normalized form.

Submission is different. When the form is submitted, URL-encoded or multipart, the spec converts every lone line feed into a carriage return plus line feed. A 280-unit message with 4 line breaks can therefore arrive at your server as 284 characters.

Browser value:   "Line one\nLine two"     -> 17 units
Server receives: "Line one\r\nLine two"   -> 18 characters

That gap breaks naive server checks and tight database columns. Normalize CRLF back to LF before you count, as the server example further down does. If you set wrap="hard", the browser also inserts its own line breaks at the column width on submit, which adds more characters again.

Does maxlength Stop Pasted Text?

Yes, for user actions. The spec lets browsers prevent the user from pushing the value past the limit, and current browsers apply that to paste as well as typing. Paste a 500-character draft into a 280-unit box and only the first part lands, with no warning.

Scripts are a different story. Setting textarea.value from JavaScript isn't truncated, and neither is text placed between the tags in your HTML. The field also won't report itself as too long, because the spec only flags values last changed by a user edit.

const box = document.getElementById("bio");
box.value = "x".repeat(400);    // accepted, all 400 units
console.log(box.validity.tooLong); // false: no user edit yet

That code fills the box with 400 characters despite the 280 limit, and validation still passes. If your app restores drafts or fills fields from an API, trim or check the value yourself before you assign it.

Build a Live Character Count Textarea in a Few Lines of JavaScript

A visible counter tells people how much room is left before the box stops them. Add an output element next to the field and update it on every input event, which fires for typing, paste, cut, drag and undo.

<label for="bio">Short bio</label>
<textarea id="bio" name="bio" maxlength="280"
          aria-describedby="bio-count"></textarea>
<p id="bio-count">0 / 280</p>
const box = document.getElementById("bio");
const count = document.getElementById("bio-count");

function updateCount() {
  count.textContent = `${box.value.length} / ${box.maxLength}`;
}

box.addEventListener("input", updateCount);
updateCount();

The result is a line under the box that reads 0 / 280 and climbs as you type. Calling updateCount() once on load matters when the box starts with text, such as a saved draft.

We prefer box.value.length here even though it shows 2 for a smiley. It uses the same normalized value and the same units as maxlength, so the counter and the limit always measure the same thing. Fill the box with smileys and a grapheme-based counter would read 140 / 280 while the field refuses more input. Users notice that kind of mismatch.

If you want both numbers, show the grapheme count as the friendly figure and keep the code-unit count for the limit. Just don't let the two drive different rules. When you're done, paste the markup into our HTML formatter to tidy the indentation before you commit it.

Making the Character Count Readable by Screen Readers

The aria-describedby="bio-count" link above means a screen reader usually reads the counter when the textarea gets focus. That covers the starting state. It doesn't announce changes while the user types.

For live updates, add a separate status element with aria-live="polite" and hide it visually with your usual screen-reader-only CSS class. Screen readers read a polite region when the user is idle instead of interrupting them.

<p id="bio-status" class="sr-only" aria-live="polite"></p>

Update that region only after a short delay, so it doesn't speak on every keystroke.

const statusEl = document.getElementById("bio-status"); // aria-live="polite"
let timer;

box.addEventListener("input", () => {
  clearTimeout(timer);
  timer = setTimeout(() => {
    const left = box.maxLength - box.value.length;
    statusEl.textContent = `${left} characters left`;
  }, 1000);
});

This announces the remaining count once, about a second after the user stops typing. The USWDS character count component takes the same approach, with a brief pause before it announces how many characters remain.

Soft Limits: Letting Users Type Past the Limit

A hard maxlength silently cuts pasted text, which hurts when someone pastes a draft they meant to trim. A soft limit drops the attribute, lets the text in, and blocks submission until it fits.

const LIMIT = 280;

box.addEventListener("input", () => {
  const over = box.value.length - LIMIT;
  box.setCustomValidity(over > 0 ? `Remove ${over} characters` : "");
  count.textContent = `${box.value.length} / ${LIMIT}`;
});

This listener replaces the earlier counter, since box.maxLength reads -1 once the attribute is gone. While the value is too long, setCustomValidity() marks the field invalid, so the form won't submit and the browser shows that text in its validation bubble. An empty string clears the error. Because you removed maxlength, this check is now the only client-side guard, so keep the server check too.

Enforce the Textarea Character Limit on the Server Too

Everything above runs in the browser, and anyone can skip it with dev tools, a script or a direct HTTP request. The server has to apply the same rule. Normalize line endings first, then count in the units you chose.

// Node.js: count the way the browser's maxlength does
function checkBio(raw) {
  const text = raw.replace(/\r\n/g, "\n");
  if (text.length > 280) return "Bio must be 280 characters or fewer.";
  return null;
}
# Python: len() counts code points, so measure UTF-16 units explicitly
def check_bio(raw):
    text = raw.replace("\r\n", "\n")
    units = len(text.encode("utf-16-le")) // 2
    if units > 280:
        return "Bio must be 280 characters or fewer."
    return None

Both functions accept anything maxlength lets a user type and reject anything longer, returning an error string you can send back to the form. Match the database column to the same rule. PostgreSQL's varchar(n) and MySQL's VARCHAR(n) both measure characters rather than bytes, so a 280-unit value always fits a 280-character column. In MySQL, use the utf8mb4 character set, because the older utf8mb3 can't store 4-byte characters such as most emoji.

If the textarea holds Markdown, apply the limit to the raw Markdown the user types, not the HTML it converts to, which is usually longer because of the added tags. Our guide to rendering Markdown in JavaScript covers the rendering side with marked and markdown-it.

A textarea character limit works best in three layers. Use maxlength for the hard stop and a counter that reads value.length so it matches that stop. Then add a server check that normalizes CRLF before counting. Get those three agreeing and users never see a counter promising room that the box won't give them. When you're drafting the help text or docs for your form, the Markdown editor shows live word, character and line counts as you write.