Validating form with jQuery and sending to a controller in codeigniter

0

I want to do a validation with jQuery before sending the data to the server side, I'm using PHP with jQuery and in the form part I use this:

 < ? php echo form_open('empresa/inserir', 'id="form-empresa"'); ?>

     < inputs ...
     < button submit / >

  < / form >

However, whenever I submit the submit, it sends it directly to the codeigniter controller, ignoring page validation.

What should I do to validate the client-side page first with jQuery, and if it's all right, call the codeigniter function?

    
asked by anonymous 23.08.2016 / 14:59

1 answer

1

Try to use the prevent-default and do your client-side validation. Remember that many fields can be validated with HTML5, as the required attribute.

$(document).ready(function() {
  $('#form-empresa').on('submit', function(e){
     var valido = false;

    // validações jquery aqui - atribua true para "valido" quando validar

    if(!valido) {
      e.preventDefault();
    }
  });
});
    
24.08.2016 / 19:45