There is a form with input that is required

<form action="" id="go"> <input type="text" name="name" class="" placeholder="set-name" required=""> <input type="submit"> </form> 

And if through javascript make submit, then the form is sent, without checking for required

 document.getElementById("go").submit(); 

And if you click on the "Send" button - then there is a check, and does not send the form until you fill out ...

Why is this, why does js not check form fields?

Chrome 57.0.2987.133

Firefox 52

    2 answers 2

    When calling the .submit() method, validation is not performed. It is assumed that when manual shipment is done independently. Therefore, do not forget to call the .checkValidity() method before sending:

     let $form = document.getElementById('form'), $submitBtn = document.getElementById('submit'); $submitBtn.addEventListener('click', function() { if( $form.checkValidity() ) { $form.submit(); } else { // ваша обработка ошибок } }); 

    A source:

    Submit method

      It is possible not to raise the submit event, but to simulate a button press event, then the check should be. With jQuery, you can do it like this:

       $("#go input[type=submit]").click();