1

Is there an easy way to test if the HTML5 required attribute is actually enforced by a browser when submitting a form? i.e. the form can't be submitted if a required field has not been filled in.

I'd rather not add modernizr just for that, and https://stackoverflow.com/a/3937924/117704 does not work for Safari: Safari does not prevent the form submission when a required field , and still document.createElement('input').required is not undefined in Safari.

I want to implement a solution like https://stackoverflow.com/a/31319587/117704 but enable it only for browsers not enforcing the required attribute.

Community
  • 1
  • 1
Florent2
  • 3,463
  • 3
  • 28
  • 38

1 Answers1

1

Check if the checkValidity() function exists:

function hasFormValidation() {

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

};

Call it like this:

if( hasFormValidation() ) {
    // Form Validation supported
};
Cas Bloem
  • 4,846
  • 2
  • 24
  • 23