0

i am stuck in a situation where i am not able to apply validation when the contents are loaded dynamically through jquery ajax call...

I found this solution everywhere but dont know how to use where to write this code??

$('form').removeData("validator");
$('form').removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse('form');

MY CODE is HERE in my previous Question

Community
  • 1
  • 1
sunnykumar
  • 37
  • 1
  • 10

1 Answers1

2

I do realise this is a old question. But it might be useful to someone who comes here in the future.

This is as simple as to run the above code after a successful ajax request.

$.ajax({
    url: editPageUrl,
    contentType: 'application/html; charset=utf-8',
    type: 'GET',
    dataType: 'html',
    data: { id: taskId },
    cache: false,
    success: function (data) {
        var taskBody = $('#task-body');
        $(taskBody).html(data);
        var $form = $(taskBody).find('form');
        $form.removeData("validator");
        $form.removeData("unobtrusiveValidation");
        $.validator.unobtrusive.parse($form);
    }
});

When the ajax request finishes just reset the validation so it works. Doing that will add all the elements to jquery validation and trigger the validation on a submit on the form inside #taskeditform-body

rjso
  • 1,314
  • 10
  • 19