jQuery

jQuery Prevent Form Submit Examples – 2 Ways

When dealing with jQuery and Forms, you may want to use jQuery to prevent form submit unless certain conditions are met. There are way to disable default form submission using jQuery and I am going to discuss 2 ways of doing it. Each of these ways have only 1 line of code so it’s super easy for you follow and implement. Read on to find out more.

How to Prevent Form Submit using jQuery – Examples

Let’s assume that you are clicking a button with ID “my_button” and don’t want the form to be submitted unless the form meets the validation conditions. In such a case, here are the 2 methods you can use to prevent form submit in jQuery.

Example 1: Prevent Form Submission using return false; in jQuery

$("#my_button").click(function () {
//Some validation code here

return false;
};

Example 2: Prevent Form Submission using e.preventDefault(); in jQuery

$("#my_button").click(function (e) {
//Some validation code here

e.preventDefault();
});

So which one to use?

I recommend using e.preventDefault() as that is more appropriate for the purpose.

Very simple, isn’t it?

Do you know of any other ways in jQuery to stop form submit? Please feel free to share your comments below.

One Comment on jQuery Prevent Form Submit Examples – 2 Ways

  1. 1

    I need help to code for ajax request and i got the solution from this article help.There is a lot of information about j query.

Leave a Reply to Ameenia Cancel reply

*

*