[removed] validate form before submit?

前端 未结 2 806
[愿得一人]
[愿得一人] 2020-12-18 00:15

I would really appreciate anyone\'s help with this as I just can\'t seem to get it working. I am using a javascript code to check my form before it is submitted. I check for

相关标签:
2条回答
  • 2020-12-18 00:51

    In the form tag there is a field onsubmit to specify your validation function. Something like that:

    <form name="myForm" onsubmit="return validateForm()" method="post">
        <!-- your inputs -->
    
    </form>
    
    0 讨论(0)
  • 2020-12-18 01:10

    You should use event.preventDefault(); in your if statement when you check for errors.

    I you are clicking a submit button, first check all the fields, if something fails in your validation, raise event.preventDefault(). With this, this will prevent the form to submit.

    If there are no errors, the preventDefault will not be raised and the form will be submitted.

    Extra information: jQuery API

    0 讨论(0)
提交回复
热议问题