Solved

jquery validation with ajaxForm

Posted on 2009-04-06
2
1,000 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses how to create an extensible mechanism for linked drop downs.
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…

763 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