Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the jQuery alternative for checkValidity()?

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?

like image 466
danday74 Avatar asked Oct 28 '25 19:10

danday74


2 Answers

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.

like image 80
Alexander Craggs Avatar answered Oct 30 '25 10:10

Alexander Craggs


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();
like image 37
Markus-Hermann Avatar answered Oct 30 '25 10:10

Markus-Hermann