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
  • 354,903
  • 100
  • 647
  • 710
tirenweb
  • 30,963
  • 73
  • 183
  • 303
  • 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,039
  • 6
  • 26
  • 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
  • 3,544
  • 1
  • 26
  • 22