Trigger standard HTML5 validation (form) without using submit button?

前端 未结 12 1417
攒了一身酷
攒了一身酷 2020-11-28 07:07

Anyone who know how I can trigger the standard HTML5 validation in a form without using a submit button? (JavaScript or jQuery).

I do not want to se

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 08:04

    You can use reportValidity, however it has poor browser support yet. It works on Chrome, Opera and Firefox but not on IE nor Edge or Safari:

    var myform = $("#my-form")[0];
    if (!myform.checkValidity()) {
        if (myform.reportValidity) {
            myform.reportValidity();
        } else {
            //warn IE users somehow
        }
    }
    

    (checkValidity has better support, but does not work on IE<10 neither.)

提交回复
热议问题