1

The code below shows an error message when a field is not filled.

As you can see I'm calling preventDefault(), but...how to call the action associated to the form if the field has been filled??

$('input#save').click(function(e){
  e.preventDefault()

  if($('#field_1').val() == ''){

    $('#error_file').show();

  }
});
Matt Ball
  • 344,413
  • 96
  • 627
  • 693
ziiweb
  • 29,901
  • 71
  • 176
  • 297
  • Consider using the [jQuery Validation plugin](http://docs.jquery.com/Plugins/validation) instead of rolling your own. – Matt Ball Aug 02 '11 at 16:54

2 Answers2

2
$('input#save').click(function(e){
  e.preventDefault()

  if($('#field_1').val() == ''){

    $('#error_file').show();

  }
  else 
  {
     $('#my_form').submit();
  }
});
Maverick
  • 3,021
  • 6
  • 24
  • 35
0

The issue here is where you've placed e.preventDefault(). You only want to prevent default if the validation fails, so just move it inside the if condition.

$('input#save').click(function(e){
    if($('#field_1').val() == ''){
        e.preventDefault();
        $('#error_file').show();
    }
});
nick
  • 657
  • 8
  • 9