Form validation - Run other code after validation

Form validation - Run other code after validation

Hello,

I am new to jquery, I have managed to pull together some validation and an ajax call on one of my pages, however I cant seem to get the ajax call to work after the validation, at the moment both calls happen at the same time so the form gets validated but the ajax call is also made. What I want happen is for the ajax call to be made only if the form is valid. I have two seperate blocks of code, one for validation and one for the ajax call. I have tried to put them both together so that the ajax call fires only after the validation but I have not had any success. Here are the two blocks of code:

Login code:


<script language="javascript">
$(document).ready(function()
{
   $('#namea').val("");
$("#login_form").submit(function()
{
  //remove all the class add the messagebox classes and start fading
//check the username exists or not from ajax
  $.post("login.php",{ name:$('#namea').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
        {
    if(data=='OK') //if correct login detail
    {
       $('#loginbox').hide();
          $('#ajax_failed').hide();
   $('#ajax_show').show();

    }
    else
    {
   $('#name').val("");
      $('#ajax_failed').show();
          }
   
        });
   return false; //not to post the  form physically
});
//now call the ajax also focus move from
$("#password").blur(function()
{
  $("#login_form").trigger('submit');
});
});
</script>

Validation Code:

<script>
  $(document).ready(function(){
  $("#login_form").validate({
  rules: {
    namea: "required",

    town: "required",

    emaila: {
      required: true,
      email: true
    }
   
   
  }
});

  });
 
  </script>

I would be grateful if anyone could point me in the right direction, I am sure its something simple but I just cant seem to grasp it and the more I read the more I confuse myself!

Thanks

JW
    • Topic Participants

    • james