HTMLInputElement: maxLength property - Web APIs | MDN

Value

A number representing the element's maxlength if present, or -1.

Example

Given the following HTML:

html

<p>
  <label for="password">Your password</label>
  <input id="password" type="password" minlength="8" maxlength="20" />
</p>

You can use the maxLength property to retrieve or set the <input>'s maxlength attribute value:

js

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 value

Specifications

Specification
HTML
# dom-input-maxlength

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.