开发者

jQuery + jQuery Form Submit Plugin + jQuery Validation Plugin

开发者 https://www.devze.com 2022-12-20 04:19 出处:网络
I realize this has been asked a few times, but wondering if you can help my situation out. Background: I\'ve tried to create a form that submits using Ajax (jQuery Form Submit). Which I have gotten t

I realize this has been asked a few times, but wondering if you can help my situation out.

Background: I've tried to create a form that submits using Ajax (jQuery Form Submit). Which I have gotten to work fine, and then I wanted to get validation on the form. Which I was able to get to work using jQuery Form Validation Plugin. But I cannot seem to get them to work together.

Basically it just submits the form weather or not it's "valid".

I'm hoping it is something simple. Are you able to see the issue?

$('#addCompany').submit(function() { 
    $('#addCompany').ajaxSubmit({
        target:        '.response',   
            beforeSubmit:  validateForm,   
        success:       function () {
            $('.response').fadeIn('slow');
        }
     }开发者_如何学编程); 

    });  //END SUBMIT FUNCTION
  }); //END DOCUMENT

  function validateForm() {
        $("#addCompany").validate({
            rules: {
                company: "required",
                contactEmail: "required"
                },
             messages: {
                company: "Please enter the clients company",
                                contactEmail: "Please enter a valid email address",
                   }
        });
    }


You doing it in wrong order. See the example here. In your case, it should be:

$("#addCompany").validate({
  rules: {
    company: "required",
    contactEmail: "required"
  },
  messages: {
    company: "Please enter the clients company",
    contactEmail: "Please enter a valid email address",
  },
  submitHandler: function(form) {
    $(form).ajaxSubmit({
      target: '.response',   
      success: function() {
        $('.response').fadeIn('slow');
      }
    });
  }
});


you need to return something from validateForm. right now it's always returning true because there is NO return value.

you want something like

return $("#addCompany").....
0

精彩评论

暂无评论...
验证码 换一张
取 消