pencilCake
pencilCake

Reputation: 53223

How to implement validation with jQuery on a asp.Net submit button?

I want to call some web-service webmethods on asp.net submit button's click in order to validate the form regarding to some business logic and then I would like to have the button to go on its default behavior if the validation is OK.

How can I stop the aspx page to post-back (after submit button click) and continue submitting the form only if it is allowed by the 'success' function of jQuery.ajax()'s option parameter?

Thanks!

Upvotes: 0

Views: 1101

Answers (1)

archil
archil

Reputation: 39491

You could pass Form Submit Callback to ajax function. If ajax executes successfully, your form submit callback will be called and form will be submitted. Otherwise, not

For ex, submit button code could look like this

function submitWithValidation() {
    $.ajax({
      url: "test.html",
      context: document.body,
      success: function(){
        $('form').submit();
      }
    });

    return false;
}

The button will stop submitting form. If validation succeeds, ajax success will submit the form, otherwise invalid form will stay with client

Upvotes: 2

Related Questions