html5 required and jQuery submit()

Here an exemple of my personnal solution:

var form = $('#loginform');

// Trigger HTML5 validity.
var reportValidity = form[0].reportValidity();

// Then submit if form is OK.
if(reportValidity){
    form.submit();
}

I did the following

<form>
    ...
    <button type="submit" class="hide"></button>
</form>

<script>
    ...
    $(...).find('[type="submit"]').trigger('click');
    ...
</script>

¡it works!


You can

  • trigger click event on a submit
  • check validation manually with $("form")[0].checkValidity()
  • find invalid elements manually using $("form :invalid")

Tags:

Html

Jquery