检测是否强制执行HTML5“required”属性

时间:2015-09-22 13:53:16

标签: html html5 forms

在提交表单时,是否有一种简单的方法可以测试浏览器是否实际强制执行HTML5 required属性?即如果尚未填写必填字段,则无法提交表格。

我不想仅为此添加modernizr,并且https://stackoverflow.com/a/3937924/117704不适用于Safari:Safari不会阻止在必填字段提交表单时仍然document.createElement('input').required不是{ Safari中的{1}}。

我希望实现类似https://stackoverflow.com/a/31319587/117704的解决方案,但仅针对未强制执行undefined属性的浏览器启用它。

1 个答案:

答案 0 :(得分:1)

检查checkValidity()函数是否存在:

function hasFormValidation() {

    return (typeof document.createElement( 'input' ).checkValidity == 'function');

};

这样称呼:

if( hasFormValidation() ) {
    // Form Validation supported
};