Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

HTML attribute: maxlength

Themaxlength attribute defines the maximumstring length that the user can enter into an<input> or<textarea>. The attribute must have an integer value of 0 or higher.

The length is measured inUTF-16 code units, which is often but not always equal to the number of characters. If nomaxlength is specified, or an invalid value is specified, the input has no maximum length.

Anymaxlength value must be greater than or equal to the value ofminlength, if present and valid. The input will fail constraint validation if the length of the text value of the field is greater than maxlength UTF-16 code units long. Constraint validation is only applied when the value is changed by the user.

Constraint validation

While the browser will generally prevent user from entering more text than the maxlength attribute allows, should the length be longer than the maxlength allows, the read-onlytooLong property of aValidityState object will be true.

Try it

<label for="name">Product name:</label><input   name="name"  type="text"  value="Shampoo"  minlength="3"  maxlength="20"  required /><label for="description">Product description:</label><textarea   name="description"  minlength="10"  maxlength="40"  required></textarea>
label {  display: block;  margin-top: 1em;}input:valid,textarea:valid {  background-color: palegreen;}

Examples

html
<input type="password" maxlength="4" />

Specifications

Specification
HTML
# attr-input-maxlength
HTML
# attr-textarea-maxlength

Browser compatibility

html.elements.input.maxlength

html.elements.textarea.maxlength

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp