Getting HTML5 Validation Errors

1

Is it possible to catch the event from a validation error of html5 in javascript ?. For example, the user did not enter a field, where it is marked as required , when it tries to send the form will generate an error popup of html5 itself, can I trigger a javascript function when this occurs?

    
asked by anonymous 24.09.2017 / 22:21

1 answer

2

One option is to use the oninvalid attribute:

<input type="text" oninvalid="handle();" required>

In your JavaScript, define the function

function handle() {
    // ...
}

See the documentation of oninvalid in w3schools.

You can use it differently. See:

element.addEventListener("invalid", function() { ... });
    
24.09.2017 / 22:29