HTMLButtonElement: validity プロパティ
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月.
validity は HTMLButtonElement インターフェイスの読み取り専用プロパティで、この要素の有効状態を表す ValidityState オブジェクトを返します。
値
ValidityState オブジェクトです。
例
次の例は、 <button> に customError が設定されている場合、不正な状態にあることを示しています。この状態では、validityState の validity プロパティは false ですが、checkValidity() は、ボタンの type が "submit" でない場合、制約検証の対象ではないため、true を返します。
js
const button = document.getElementById("myButton");
button.setCustomValidity("This button is invalid.");
const validityState = button.validity;
console.log(validityState.valid); // false
console.log(validityState.customError); // true
console.log(button.checkValidity()); // ボタンの種類が "submit" の場合は false、そうでなければ true
仕様書
| Specification |
|---|
| HTML> # the-constraint-validation-api:dom-cva-validity> |
ブラウザーの互換性
Loading…