ElementInternals: validity プロパティ
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2023年3月.
validity は ElementInternals インターフェイスの読み取り専用プロパティで、制約検証に関して要素が取り得るさまざまな検証状態を表す ValidityState オブジェクトを返します。
値
ValidityState オブジェクトです。
例外
NotSupportedErrorDOMException-
要素が
formAssociatedプロパティがtrueに設定されていない場合に発生します。
例
次の例は、formAssociated を true に設定したカスタムチェックボックスコンポーネントを表示させており、validity.valid の値がコンソールにログ出力されます。
js
class CustomCheckbox extends HTMLElement {
static formAssociated = true;
constructor() {
super();
this.internals_ = this.attachInternals();
}
// …
}
window.customElements.define("custom-checkbox", CustomCheckbox);
let element = document.getElementById("custom-checkbox");
console.log(element.internals_.validity.valid);
仕様書
| Specification |
|---|
| HTML> # dom-elementinternals-validity> |
ブラウザーの互換性
Loading…