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 2015年7月.
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> |