Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. ValidityState
  4. tooLong

ValidityState: tooLong property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since December 2018.

The read-onlytooLong property of theValidityState interface indicates if the value of an<input> or<textarea>, after having been edited by the user, exceeds the maximum code-unit length established by the element'smaxlength attribute.

Value

A boolean that istrue if theValidityState does not conform to the constraints.

Examples

Textarea with too long character count

The following example checks the validity of atextarea element.A constraint has been added using themaxlength attribute so the textarea expects a maximum of 10 characters.If there are too many characters in the textarea (which is true below), the element fails constraint validation, and the styles matching:invalid CSS pseudo-class are applied.

When editing the textarea, the browser will not allow the user to add characters that would fail constraint validation of maximum character count, so at first, only deleting characters is allowed.Newline characters are normalized and count as a single character in the maximum length calculation.

css
textarea:invalid {  outline: red solid 3px;}
body {  margin: 0.5rem;}textarea {  width: 75%;}pre {  padding: 1rem;  height: 2rem;  background-color: lightgrey;  outline: 1px solid grey;}
html
<pre>Validation logged here...</pre><textarea name="story" maxlength="10" rows="5">It was a dark andstormy night...</textarea>
js
const userInput = document.getElementById("userText");const logElement = document.getElementById("log");function log(text) {  logElement.innerText = text;}userInput.addEventListener("input", () => {  userInput.reportValidity();  if (userInput.validity.tooLong) {    log("Too many characters in the textarea.");  } else {    log("Input is valid…");  }});

Specifications

Specification
HTML
# dom-validitystate-toolong-dev

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp