Search code examples
phpjqueryajaxscriptaculous

Jquery Ajax and Scriptaculous Form Validation


I have a form that is using scriptaculous form validation but i am using jquery ajax. The form validation works fine before i add the ajax. Once i add the ajax it is like the form no longer validates. The ajax just sumbits the form with no validation. It is like the validation is just bypassed by the ajax.

      <script type="text/javascript">
      jQuery('form').delay(3000).submit(function() {
      string = jQuery("form").serializeArray();

      jQuery.ajax({
    type: "POST",
    url: "../script_insert.php",
    data: string,
    dataType: "html",
    success: function(html){
      jQuery('#Right_Content').hide().html(html).fadeIn("slow");;

       }
       })
       return false;
       });
       </script>

But when i remove the ajax from the page all together it works fine. The page validates. Can anyone tell me how to make sure the form validation takes place before the ajax submit. I tried the jquery delay but this does not work either. Thanks.


Solution

  • Why don't you add validation as the first thing that .submit() does? That way you're sure the form is validated no matter how many form fields have had focus.

    jQuery('form').submit(function() {
        if (validate()) {
            string = jQuery("form").serializeArray();
            ...
        } else {
            return false;
        }
    });