skip to Main Content

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?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search