Solved

jquery validation with ajaxForm

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JQuery & CSS Tab Issues 7 44
Jquery autocomplete 10 67
jQuery Parallax 1 38
popup load of page and setting up of session 8 22
Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

948 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

22 Experts available now in Live!

Get 1:1 Help Now