I have an Ajax form in my MVC application that is validated using jquery validation. Initially the validation wasn't working because the date fields where in a UK format instead of US (which is by design as I'm UK based), so I added some code to stop that.
Jquery Validation Date
$(function () {
$.validator.addMethod("date", function(value, element) {
var dateReg = /^\d{2}([./-])\d{2}\1\d{4}$/;
return value.match(dateReg);
}, "Invalid date");
$('#form').validate({
submitHandler: function(form) {
form.submit();
}
});
});
The code I added however hijacks the form.submit which means the ajax form doesn't trigger it's success or failure functions.
Ajax Form Code
@using (Ajax.BeginForm("Create", "Fixtures", new AjaxOptions
{
OnSuccess = "Success",
OnFailure = "Failure"
}, new { @class = "standard-form", @Id = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
...Form fields etc...
}
Script
function Success() {
console.log("Success")
}
function Failure() {
console.log("Failure")
}
The whole way this is doesn't doens't feel clean to me, so my question is how do I validate the form correctly (using jquery validate or an alternative) but also retain the success and failure functions of my ajax form?