Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. HTMLInputElement
  4. maxLength

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.

Value

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

Example

Given the following HTML:

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:

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 byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp