HeySatan HeySatan - 1 month ago 12
Javascript Question

Kendo MVC cancel form submission - form always submits

I am looking for a way to cancel a form submission via Javascript when using Telerik Kendo MVC. The code I have doesn't work, I can't figure out how to prevent the form submission - it always submits.

I have a classic MVC form:

@using (Html.BeginForm("CreateApplication", "Home"))

with



and the following JS:

$("form").kendoValidator({
validate: function (event) {

var errors = this.errors();

if (errors.length > 0) {
showErrors(this.errors());
event.preventDefault();
} else {
if (! checkCostCode() || !checkApplication()) {
event.preventDefault();
}
}
}
});


The methods checkCostCode() and checkApplication() return false, yet the form still submits.

Can anyone help?

Thanks in advance.

Answer

I figured it out using:

$(document).ready(function () {

    kendo.culture("en-GB");

    $("form").kendoValidator({
        validate: function (event) {

            var errors = this.errors();

            if (errors.length > 0) {
                showErrors(this.errors());
               event.preventDefault();
            } else {

                if (!checkCostCode()){
                    isValid = false;
                }

                if (!checkApplication()) {
                    isValid = false;
                }
            }
        }
    });

    $("form").submit(doSubmit);
});

function doSubmit() {
return isValid;
}
Comments