Link to home
Start Free TrialLog in
Avatar of EdMacFly
EdMacFly

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EdMacFly
EdMacFly

ASKER

Well that was straight forward enough!

How would I change the code so that an alert was fired every time the validation failed?
And thank you :)
Easy to follow and prompt. Good answer.
>>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;
}