Catching HTML 5 validation errors

Asked

Viewed 96 times

1

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

1 answer

2


One option is to use the attribute oninvalid:

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

In your Javascript set the function

function handle() {
    // ...
}

View documentation of oninvalid in w3schools.

You can use it differently. See:

element.addEventListener("invalid", function() { ... });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.