I've just discovered the JavaScript Validation API checkValidity() function.
caniuse.com says this API is available in Firefox and Chrome. I do not need support for other browsers.
Does jQuery provide its own implementation of this API?
Wanting something from Javascript for jQuery? Just add it to the jQuery namespace like so:
$.fn.isValid = function(){
  return this[0].checkValidity()
}
Then you can just use $("#id").isValid() to return either true/false depending on the validity.
Credit to @Andrew Whitaker.
The accepted answer is good. A slight point of criticism being that preliminary code has to be executed to alter that namespace. Offering a second approach, accessing the wanted function via the DOM attached to the jquery object. Use .get(..). E.g., like so:
let form = $('#my_form_to_be_validated');
let is_valid = form.get(0).checkValidity();
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