Home > OS >  Call javascript function after form validation ASP.NET MVC
Call javascript function after form validation ASP.NET MVC

Time:06-30

I have set an OnSubmit() property to @Html.BeginForm() that calls this function:

    $('form').submit(function () {
        deleteImages();
        return true;
    });

The problem is, this gets executed even if the form isn't valid. How do I check if the form is valid, then call the function?

CodePudding user response:

You can try ubobtrusive validation (if you are using Jquery)

$('form').removeData("validator").removeData("unobtrusiveValidation");
        $.validator.unobtrusive.parse($('form'));
        if ($('form').valid()) {
            deleteImages();
            return true;
        }

This might help.

CodePudding user response:

I don't know the logic in your application, but if you are afraid of someone messing with you JS, then you should do validation on the server as well (Its always better to have validation on both - client and server side)! Validate it on the server and return a flag or something, then you will know that you have to execute deleteImages()

  • Related