How to check that the unobtrusive validations has been validated in jquery function?
Asked Answered
K

1

5

I have a form with four fields. I have applied some unobtrusive validations to 3 of them. I want that when all unobtrusive validation have been performed then a jquery function is called, but there I have defined an onsubmit event on the form so that every time it first goes to that jquery function and perform all the steps then shows that the unobtrusive validation messages for the respective text boxes.

But I want to perform all steps of the jquery function if the form has passed the unobtrusive validation.

Is there a way to check that all unobtrusive validation has been performed?

This is my jquery code:

function submitDetails()
{
var check = $('#flag').val();            
if (check == 1)
{
return true;
}

else {
var birthDate = ($('#BirthDate').val()).split('/');
var graduationDate = ($('#GraduationDate').val()).split('/');
var stdate = birthDate[2] + birthDate[1] + birthDate[0];
var endate = graduationDate[2] + graduationDate[1] + graduationDate[0];
if (($('#LastName').val() == "") || (parseInt(endate) < parseInt(stdate)))
  {
    $('#Window').data('tWindow').center().open();
    return false;
}
else { return true; }
}
}   
Kimberli answered 23/11, 2011 at 6:54 Comment(0)
S
11

You could check if the form is valid in the submit handler:

$('#formId').submit(function() {
    if (!$(this).valid()) {
        // validation failed => here you can call your function or whatever
        return false;
    } else {
        // the form is valid => you could perform some other action if you will
    }
});
Samuelsamuela answered 23/11, 2011 at 7:10 Comment(2)
Thanks Darin, I have tried this, without calling any function on onsubmit event, but here also it first checks the unobtrusive validation when i click on submit and if all validation get satisfied then it move to post action. And do not go to the jquery function as you have suggested, where are need to check some steps. thanks..Kimberli
I am giving form Id as follows : @{Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "formId" });} //form body <input type="submit" id ="submit_button" value="Create" name = "Create" /> @{Html.EndForm();} and at the start of page defined the jquery in the head section as what you have suggested But the code is not going to execute the steps of Jquery.Kimberli

© 2022 - 2024 — McMap. All rights reserved.