HTMLOutputElement: setCustomValidity() 方法
setCustomValidity()
方法属于 HTMLOutputElement
接口,用于为 <output>
元素设置自定义的有效性消息。使用空字符串表示该元素没有自定义有效性错误。
<output>
元素不是约束验证的候选对象。reportValidity()
方法不会导致向用户显示自定义错误消息,但会将其元素 ValidityState
对象的 customError
属性设置为 true
,并将 valid
属性设置为 false
。
语法
js
setCustomValidity(string)
参数
string
-
包含错误消息的字符串。空字符串会移除任何自定义有效性错误。
返回值
无(undefined
)。
示例
在此示例中,如果 <output>
的 value
不是非零数字,我们会设置一个自定义错误消息。如果它是一个数字,我们会将自定义错误设置为空字符串。
js
const cart = document.getElementById("cart-form");
const total = cart.elements("total");
if (parseFloat(total.value)) {
errorOutput.setCustomValidity("");
} else {
errorOutput.setCustomValidity("There is an error");
}
规范
规范 |
---|
HTML # dom-cva-setcustomvalidity-dev |
浏览器兼容性
加载中…