0

I am new to MVC validation. I was trying to ignore the validation when I change the index in dropdownlist with submit form. However, if I have another submit button in the from fired the validation, the method below would not work.

dropdown list :

    $("#dropdown").change(function () {

        $('#MyForm').validate().cancelSubmit = true;
        $('#MyForm').submit();
    });

another button:

    $("#Button").click(function () {
      $('#MyForm').removeData('validator');
      $('#MyForm').removeData('unobtrusiveValidation');
      $.validator.unobtrusive.parse($('#MyForm'));
      $('#MyForm').submit();
    });

Because I have some of html elements are dynamically genrated by ajax call, and I want to validate those dynamic elements also, when I hit submit. This is why I am doing this:

      $('#MyForm').removeData('validator');
      $('#MyForm').removeData('unobtrusiveValidation');
      $.validator.unobtrusive.parse($('#MyForm'));
0

3 Answers 3

4

Finally, I did something below, I solved the issue.

$("#dropdown").change(function () {
    $('#MyForm').removeData('validator');
    $('#MyForm').removeData('unobtrusiveValidation');
    $('#MyForm').validate().cancelSubmit = true;
    $('#MyForm').submit();
});
2
@using (Html.BeginForm())
{
    { HtmlHelper.ClientValidationEnabled = false;}
}

or

$('input, textarea, select', 'form').each(function () {
    $(this).attr('data-val', 'false');
});
2
  • If I did this, all validations in the form would be disabled. At the moment, I just want to disable the validation when I change the index of this special dropdownlist. Other buttons or dropdownlist or links can still fire validation. Jun 16, 2014 at 21:52
  • $('input, textarea, select', 'form').each... put this inside function and while index of your special dropdownlist changed call method and disable validation and simply when index changed again re-enable validation by calling another function. inside function change data-val value to true ($(this).attr('data-val', 'true')); Jun 17, 2014 at 6:28
0

You can also add special attribute to form submit button:

<button type="submit" formnovalidate>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.