最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the jQuery alternative for checkValidity()? - Stack Overflow

programmeradmin2浏览0评论

I've just discovered the JavaScript Validation API checkValidity() function.

caniuse 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?

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?

Share Improve this question edited Sep 20, 2021 at 8:43 danday74 asked Sep 20, 2016 at 11:39 danday74danday74 57k55 gold badges267 silver badges331 bronze badges 3
  • i should add - i dont want to use a jQuery plugin - thanks – danday74 Commented Sep 20, 2016 at 11:41
  • Why do you not want to use the widely used jQuery validation plugin? And if not, use it as plain JS – mplungjan Commented Sep 20, 2016 at 11:43
  • 2 This question makes little sense. Just because you are using jQuery “heavily,” does not mean that you can not use normal DOM functionality. – C3roe Commented Sep 20, 2016 at 11:47
Add a comment  | 

2 Answers 2

Reset to default 14

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();
发布评论

评论列表(0)

  1. 暂无评论