Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

maxlength — maximum text length

The maxlength attribute limits text length in textarea and selected input types. It counts UTF-16 code units rather than user-perceived characters. It does not limit a numeric value or automatically truncate text assigned by a script.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

maxlength="N"

maxlength="N" specifies the maximum text length. N must be a non-negative integer; 0 is allowed. The attribute applies to textarea and these input types: text, search, tel, url, email, and password. It does not apply to number: max limits a numeric value, not its digit count.

Length is measured in UTF-16 code units, not user-perceived characters: some emoji occupy two units. In textarea, each normalized line break counts as one unit.

Omitting the attribute means this limit is absent. If minlength is also specified, its value must not exceed maxlength. maxlength alone does not require a nonempty field; use required for that.

The browser may prevent entry of overlong text. The validity.tooLong state requires an exceeded limit, a set dirty value flag, and a last change made by the user. Assigning .value through a script does not truncate the text or itself set this error, as the example demonstrates. Client-side constraints do not replace server-side validation.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>maxlength</title>
<label for="tag">Tag (maximum 4 UTF-16 code units)</label>
<input id="tag" type="text" maxlength="4">
<pre id="result"></pre>
<script>
const tag = document.getElementById('tag');
tag.value = 'ABCDE';
document.getElementById('result').textContent =
  `${tag.maxLength}\n${tag.value}\n${tag.validity.tooLong}`;
</script>
</html>

Result

4
ABCDE
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources