What HTML attribute defines the maximum number of characters allowed in an input?

The story behind the answer

The HTML attribute that defines the maximum number of characters allowed in an input is `maxlength`.

Applied to text-like controls, `maxlength` sets an upper limit on the length of the user-entered value. For example, `<input maxlength="20">` permits a value of up to 20 UTF-16 code units. Browsers commonly stop ordinary typing once the limit is reached, and the field can also fail constraint validation if its value exceeds the declared maximum.

The most common confusion is with `size`, which controls the visible width of a text input rather than the permitted value length. `minlength` establishes a lower bound, while `pattern` checks whether text matches a specified regular-expression pattern. None of those attributes performs the same job as `maxlength`.

The attribute can be paired with `minlength`, provided the maximum is not smaller than the minimum. It also works on `<textarea>`. “Characters” is a useful shorthand, but Unicode details matter: some symbols occupy more than one UTF-16 code unit, so the browser’s technical count may not exactly match what users perceive as individual characters. Server-side validation is still necessary because client-side limits can be bypassed.

Source: Wikipedia · fact-checked Aug. 2026

Add question to a list

Choose a list to keep this question in: