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 December 2018.
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 |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
validity |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.