HTMLTextAreaElement: setCustomValidity() メソッド

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.

setCustomValidity()HTMLTextAreaElement インターフェイスのメソッドで、 <textarea> 要素の独自の検証メッセージを指定します。空文字列を使用すると、この要素に独自の検証エラーがないことを示します。

構文

js
setCustomValidity(string)

引数

string

エラーメッセージが格納されている文字列。空文字列を指定すると、独自の検証エラーがすべて除去されます。

返値

なし (undefined)。

この例では、もし <textarea> が制約検証を合格しなかった場合、検証を通らなかった制約に基づいて独自エラーを指定します。値が有効な場合は、独自エラーを空文字列に設定します。

js
const comment = document.getElementById("comment");
if (comment.validity.valueMissing) {
  comment.setCustomValidity("空のコメントを送信することはできません。");
} else if (comment.validity.tooShort) {
  comment.setCustomValidity("もっと伝えてください。コメントが短すぎます。");
} else if (comment.validity.tooLong) {
  comment.setCustomValidity(
    "おしゃべり好きですか? 800 文字以内に収めてください!",
  );
} else {
  comment.setCustomValidity("");
}

仕様書

Specification
HTML
# dom-cva-setcustomvalidity-dev

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
setCustomValidity

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

関連情報