Solved

jquery validation with ajaxForm

Posted on 2009-04-06
2
995 Views
Last Modified: 2012-05-06
Hello,

I have combined jquery ajaxForm and validation plugins and it works... it only submits the form when the form is valid. I did it like this:

<script type="text/javascript">
$(document).ready(function () {
    $('#submitForm').click(function() {
        if ($('#contactForm').valid()) {
            $('#loading').fadeIn("slow");
            $('#contactForm').ajaxForm(function(data) {
                if (data==1){
                    $('#success').fadeIn("slow");
                    $('#contactForm').resetForm();
                    $('#loading').fadeOut("slow");
                    $('#success').animate({opacity: 1.0}, 2000)
                    $('#success').fadeOut("slow");
                  }
                else if (data==2){
                    $('#badserver').fadeIn("slow");
                    $('#loading').fadeOut("slow");
                }
            });
        }
    });
});



Thanks
</script>

Is there a better way to do this? Or can you see and fault with this code?

Thanks
0
Comment
Question by:Steggs
2 Comments
 
LVL 16

Accepted Solution

by:
anoyes earned 500 total points
ID: 24091736
It looks fine to me.  The only thing I might change would be to chain some of your fades, so that one element fades in after the previous element has faded out, instead of the same time.  So for example in your failure part, I might do something like this:

else if (data==2){
  $('#loading').fadeOut('slow',function() {
    $('#badserver').fadeIn('slow');
  };
}

This ensures that your loading message is gone before you load the error message.  That's relatively minor and really just a personal preference, but other than that the script looks fine.
0
 
LVL 30

Author Comment

by:Steggs
ID: 24092938
Thanks anoyes, that chaining works better
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question