HTMLInputElement: maxLength property
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
ThemaxLength property of theHTMLInputElement interface indicates the maximum number of characters (inUTF-16 code units) allowed to be entered for the value of the<input> element, and the maximum number of characters allowed for the value to be valid. It reflects the element'smaxlength attribute.-1 means there is no limit on the length of the value.
Note:Browser generally prevent users from entering more characters than themaxlength attribute allows. Should the length be longer, the element is considered invalid and theValidityState object'stooLong property will betrue.
In this article
Value
A number representing the element'smaxlength if present, or-1.
Example
Given the following HTML:
<p> <label for="password">Your password</label> <input type="password" minlength="8" maxlength="20" /></p>You can use themaxLength property to retrieve or set the<input>'smaxlength attribute value:
const inputElement = document.querySelector("#password");console.log(`Element's maxLength: ${inputElement.maxLength}`); // "Element's maxlength: 20"inputElement.maxLength = 18; // updates the element's maxlength attribute valueSpecifications
| Specification |
|---|
| HTML> # dom-input-maxlength> |