Solved

jquery validation with ajaxForm

Posted on 2009-04-06
2
986 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now