user982124 user982124 - 1 month ago 14
Javascript Question

jQuery Form Submit Validation Not Submitting

I have a simple validation when a form is submitted to check for the present value of a hidden input, and if that is empty to show an alert:

$("form").submit(function(event) {
event.preventDefault();
if ($('#productID').val() == '') {
$("#alert_ajax_error").show();
return false;
}
});


This works when no selection have been made and prevents the form from being submitted, however if a selection has been made it is not actually submitting the form.

Answer

Move event.preventDefault(); inside your IF statement

$("form").submit(function(event) {

  if ($('#productID').val() == '') {
    event.preventDefault();
    $("#alert_ajax_error").show();
    return false;
  }

});

event.preventDefault() prevents the default submit behaviour of the form which you put it above this will always execute.

hope that helps .....