How to check if a
is valid with unobtrusive

2019-08-24 04:08发布

I have a form which loads a div on client side. I have hard coded textbox controls with all validation attributes, similar to what it renders when loaded from server. Inside the div there is submit button, but when I click on submit all validations messages on the form are displayed. I just need only the div elements validation messages to be shown. Telerik Grid control in ajax mode, does similar thing, i.e., appends textboxes with hardcoded validation attributes on client side, but it manages to fire validations only for the grid not entire form. I think I am missing something here.

$('#div').valid() --> doesn't work
$('#form').valid() --> works

1条回答
孤傲高冷的网名
2楼-- · 2019-08-24 05:07

You need to parse the validation rules of the containing form once you add the div to the DOM:

var form = $('#div').closest('form');
form.removeData('validator');
form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse(form);

and here's a live demo.

查看更多
登录 后发表回答