Malin Dharmaratne
Malin Dharmaratne

Reputation: 5

Jquery form validation - How not to execute code before validating

I have written a small form with some calculations. Now the calculation are done using button click event.

  $('a#check_var').click(function() {
    // the below call the form validation  
    $("#bet_cal_form").valid();
          <- My code here ->
  });

Now everything works, problems is I don't want my code to execute unless there are no error in the form validation. Currently I get the error messages but also my code get executed.

Upvotes: 0

Views: 120

Answers (2)

mplungjan
mplungjan

Reputation: 177786

You need an if and to cancel the link and since all IDs need to be unique, you do not need the a in the selector

$(function() {
  $("#check_var").on("click",function(e) {
    e.preventDefault();
    // the below call the form validation  
    if($("#bet_cal_form").valid()) {
      <- My code here ->
    }
  });
});

Upvotes: 0

Barmar
Barmar

Reputation: 780842

Use an if statement, of course.

if ($("#bet_cal_form").valid()) {
    // Stuff that should only be done if form is valid
}

Upvotes: 1

Related Questions