ElementInternals: validity プロパティ

Baseline 2023
Newly available

Since March 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

validityElementInternals インターフェイスの読み取り専用プロパティで、制約検証に関して要素が取り得るさまざまな検証状態を表す ValidityState オブジェクトを返します。

ValidityState オブジェクトです。

例外

NotSupportedError DOMException

要素が formAssociated プロパティが true に設定されていない場合に発生します。

次の例は、formAssociatedtrue に設定したカスタムチェックボックスコンポーネントを表示させており、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

ブラウザーの互換性

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
validity

Legend

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

Full support
Full support