Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Form Validation before Ajax submit

JavaScript bit:

$(document).ready(function()     {             $('#form').submit(function(e)             {                       e.preventDefault();                 var $form = $(this);                  // check if the input is valid                 if(! $form.valid()) return false;                     $.ajax(                     {                     type:'POST',                     url:'add.php',                     data:$('#form').serialize(),                     success:function(response)                     {                         $("#answers").html(response);                     }                 });                   })     }); 

HTML bit:

    <input type="text" name="answer[1]" class="required" />     <input type="text" name="answer[2]" class="required" /> 

So this is the code I am trying to use. The idea is to get all my inputs validated before I send my form with Ajax. I've tried numerous versions of this now but every time I end up with submitting even though the form is not entirely filled out. All my inputs are of the "required" class. Can anyone see what I am doing wrong?

Also, I depend on class-based requirements as my input names are generated with php so I can never be sure what name[id] or input types I get.

I show/hide questions as I go through it in "pages".

<input type="button" id="next" onClick="toggleVisibility('form3')" class="next-btn"/> 

JS:

function toggleVisibility(newSection)          {             $(".section").not("#" + newSection).hide();             $("#" + newSection).show();         }  
like image 270
Tom Avatar asked Jul 13 '12 11:07

Tom


People also ask

How to validate form through AJAX?

Enable the submit button in the input form. If the <valid/> element value is false, set the HTML of the validationMessage div element in Catalog ID field row to "Catalog Id is not Valid". Disable the submit button, and set the values of the other input fields. Next, run the Ajax application in JDeveloper 10.1.

What is AJAX form submission?

AJAX form submitting allows you to send data in the background, eliminating the need to reload websites to see the updates. This makes the user experience much smoother.


2 Answers

You could use the submitHandler option. Basically put the $.ajax call inside this handler, i.e. invert it with the validation setup logic.

$('#form').validate({      ... your validation rules come here,      submitHandler: function(form) {         $.ajax({             url: form.action,             type: form.method,             data: $(form).serialize(),             success: function(response) {                 $('#answers').html(response);             }                     });     } }); 

The jQuery.validate plugin will invoke the submit handler if the validation has passed.

like image 175
Darin Dimitrov Avatar answered Sep 20 '22 11:09

Darin Dimitrov


first you don't need to add the classRules explicitly since required is automatically detected by the jquery.validate plugin. so you can use this code :

  1. on form submit , you prevent the default behavior
  2. if the form is Invalid stop the execution.
  3. else if valid send the ajax request.
$('#form').submit(function (e) {   e.preventDefault();   var $form = $(this);    // check if the input is valid using a 'valid' property   if (!$form.valid) return false;    $.ajax({     type: 'POST',     url: 'add.php',     data: $('#form').serialize(),     success: function (response) {       $('#answers').html(response);     },   }); }); 
like image 36
amd Avatar answered Sep 20 '22 11:09

amd