To check whether a certain field is valid, use:
检查某个输入是否有效:

$('#myField')[0].checkValidity(); // returns true/false

To check if the form is valid, use:
检查表单是否有效:

$('#myForm')[0].checkValidity(); // returns true/false

If you want to display the native error messages that some browsers have (such as Chrome), unfortunately the only way to do that is by submitting the form, like this:
如果需要显示一些浏览器(如Chrome)的原生错误信息,唯一的方法是通过提交表单:

var $myForm = $('#myForm');

if(! $myForm[0].checkValidity()) {
  // If the form is invalid, submit it. The form won't actually submit;
  // this will just cause the browser to display the native HTML5 error messages.
  $myForm.find(':submit').click();
}

Hope this helps. Keep in mind that HTML5 validation is not supported in all browsers.
希望这有帮助。请记住,并非所有的浏览器都支持HTML5验证。

How to force a html5 form validation without submitting it via jQuery

标签: none

添加新评论