Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger validationEngine with $.ajax submit?

I'm using an inline function to submit a form but it's not calling the validation script I have and I wondered if anyone could point me in the right direction of an answer?

My button:

<a href="javascript:UpdateEnquiry();" id="continue_btn" class="btnSprite">Save</a>

My Function:

function UpdateEnquiry() {

$.ajax({
  url: 'test.php',
  dataType: 'xml',
  timeout: 15000,
  type: 'post',
  data: $('#validate').serialize(),
  success: UpdateEnquirySuccess,
  error: function (result) {parent.$.fancybox.close();}
  });
}

And I would like to initialise the jquery validationEngine plugin before submit. http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

Any help would be hugely appreciated.

Kind regards Rachel

like image 502
Rachel Avatar asked May 05 '26 12:05

Rachel


2 Answers

Assuming your validation engine is already initialized, call the validate method before doing the ajax request. If that method returns true, you proceed:

function UpdateEnquiry() {
    if ( $(yourForm).validationEngine('validate') ) {
        $.ajax({
            ...
        });
    }
    else {
        // The form didn't validate
    }

    return false; // Prevents default action from happening
}
like image 162
mgibsonbr Avatar answered May 07 '26 06:05

mgibsonbr


$('#registerform').submit(function(e) {
                e.preventDefault();
                var vars = $("#registerform").serialize();

                if ($("#registerform").validationEngine('validate')) {

                    $.ajax({
                            url:"sample.php"
                    });

                }
            });

without stopping default action i was redirecting to my form action page. So had to use preventdefault(). @mgibsonbr my bad

like image 24
jack Avatar answered May 07 '26 06:05

jack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!