Solved

jquery validation with ajaxForm

Posted on 2009-04-06
2
1,004 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JQuery Detecting page post back 6 33
Increase counter and attr inside a while loop 15 49
boostsrap 1 34
Add Promise to Existing Code 6 28
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

732 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