If i have an input like <input type="text">
and i want to trigger a native error on the input, can you do that? Like (faux code) ele.triggerError('Error Message');
It would then look like:
(source: tylergaw.com)
But with a custom message and for it's own validation function. Needed for AJAX validations for example.
Using HTML5, we can create a form with built in validation (i.e. no javascript required). Earlier, we were using JAVASCRIPT to control form validation.
HTML5 introduced a new HTML validation concept called constraint validation. HTML constraint validation is based on: Constraint validation HTML Input Attributes. Constraint validation CSS Pseudo Selectors.
There are three categories of HTML5 form validation features: HTML attributes on <input> , <select> , and <textarea> elements. (From here on out, I will just refer to them all as <input> cause brevity) CSS pseudo selectors.
The only way to trigger the native error is to submit the form. Although you can set a custom message with setCustomValidity
(as described in my answer here) and you can trigger the invalid
event with checkValidity
, this only provides hooks for you to create your own validation UI. Here's a simple example you can play around with to verify.
Note that if you submit the form with the submit()
method that will bypass the validation API. But if you trigger the click
event of the submit button that will work in Firefox and Opera, but not Chrome. I'd avoid doing it right now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With