Submit form with jQuery.Validation

Asked

Viewed 436 times

1

I want to create a similar method to the required jQuery.Validate just to check if the field has been completed or not.

Because the form is too big and can not be filled out at once so the user will save and then edit again so with this method he can know faster what is missing or not.

I created a method that "worked" validation but it does not allow submitting the form. What can I do?

  • What language do you use in server-side? PHP ?

  • 1

    And what’s your current code so we can help solve your problem?

  • You managed to solve the problem?

1 answer

1

If you’re using a javascript function, you’ve probably done something like:

 <form onsubmit="return validateForm()" method="post">

 function validateForm() {
     // logica para validar formulario
     if( ... )
     {
         return true;
     }
     return false;
 }

In this construction you need to return one true to submit the form. If your function is broken, it may not return or true nor false, which would not send the form. Before returning place a alert to check the result and make sure there are no holes in the script.

If you are writing with jquery validation pluggin, you need to mount the objects correctly. Surely the function already makes this return correctly for you.

A way to build manually would be:

// pode ser a forma abaixo, que já caiu em desuso, ou a mais atual ainda abaixo
//$( "#idDoFormulario" ).submit(function( event ) {
$( "#idDoFormulario" ).on("submit", function( event ) {

   // a função é chamada antes de enviar,
   //então você irá ver a mensagem antes de ir pro servidor
   alert( "Antes de enviar foi chamado." );

   // você pode usar alguma destas maneiras abaixo para impedir o envio
   event.preventDefault(); // impede a ação padrão, que é enviar o formulário
   return false; // também impede 

   // se você não retornar nada ou um true, o formulário é enviado
});

Browser other questions tagged

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