Validating form with jQuery and sending to a controller in codeigniter

Asked

Viewed 278 times

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 use it:

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

     < inputs ...
     < button submit / >

  < / form >

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

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

  • What Data Types Need to Validate?

  • Wouldn’t it be easier to use the codeigniter form_validation library? It works very well. If you don’t know how to use it, give me a call.

  • So I need to validate some input type text fields, type name, email ... but if I use the form_validation of codeigniter I will be validating by server side, and I wanted to validate by client side, will not have any method?

1 answer

1


Try using the Prevent-default and perform its client-side validation. Remembering that many fields can be validated with HTML5, such 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();
    }
  });
});
  • That’s what I wanted to do, I hadn’t thought about this preventDefault, and about the validation of HTML5 I wanted to do something more stylized with jQuery so I didn’t use it! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.