My problem is with the validation of my form using Jquery. My validation was working good with the validate function only but then I added the submit option and the validation is not taking place. I would like to validate my form and then execute the submit.
$(document).ready(function () {
$('#ReqCreateForm').validate({
invalidHandler: function (e, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
var message = errors == 1
? 'Missing 1 field'
: 'Missing fields';
$("div.error span").html(message);
$("div.error").show();
} else {
$("div.error").hide();
}
},
onkeyup: false,
highlight: function (element) {
$(element).addClass('error');
}, unhighlight: function (element) {
$(element).removeClass('error');
}
});
$('#ReqCreateForm').submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
alert("All Great!");
},
error: function (jqXhr, textStatus, errorThrown) {
alert("Error '" + jqXhr.status + "' (textStatus: '" + textStatus + "', errorThrown: '" + errorThrown + "')");
}
});
return false;
});
});
Thanks in advanced.
Solution:
The solution was very simply. Added the following code to my submit option, before $.ajax.
if (!$(this).valid()) {
return false;
}