jQuery + ASP + Validation + ajaxform

I have written some jQuery which is intended to validate a simple html form before sending the form via email using asp. The validation seems to work as the error messages appear on the form but for some reason the form is still being sent if the data entered is not valid. Does anyone have any idea where I am going wrong? TIA
//@Additions by EJS - jQuery + ASP email
//AJAX Sending of ASP
//+ Basic Validation

jQuery(document).ready(function() { 
        var options = {
                beforeSubmit:  showRequest,  // pre-submit callback 
                success:       showResponse  // post-submit callback 
        }; 

        // bind form using 'ajaxForm' 
        jQuery('#contactform').ajaxForm(options); 
});

function showRequest() {

    jQuery("#contactform").validate({
        //set the rules for the field names
        rules: {
            name: {
                required: true,
            },

            mememail: {
                required: true,
                email: true
            },

            message: {
                required: true
            }
        }
    });

    jQuery("#contactform").validate().form();
}

function showResponse() {
    alert("Thanks for your message!");
    disablePopup();
    jQuery('#contactform').clearForm();
}

Open in new window

EdMacFlyAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
you need to return either true or false, depending on whether or not the input is valid. So change:
...
 jQuery("#contactform").validate().form();
}

to:
...
return  jQuery("#contactform").validate().form();
}

since the form() method returns true when the form input is valid - false otherwise.
0
 
EdMacFlyAuthor Commented:
Well that was straight forward enough!

How would I change the code so that an alert was fired every time the validation failed?
0
 
EdMacFlyAuthor Commented:
And thank you :)
0
 
EdMacFlyAuthor Commented:
Easy to follow and prompt. Good answer.
0
 
hieloCommented:
>>How would I change the code so that an alert was fired every time the validation failed?
In that case save the returned value of the form() method first. IF it is false, then do the desired alert. At the very end simply return the saved returned value:
...
  var temp =  jQuery("#contactform").validate().form();
  if(!temp)
  {
    alert("Invalid");
  }
return temp;
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.