Dynamically Add Required Attribute for MVC3 jQuery Unobtrusive

While you can add the data-val attributes for MVC client side validation by default MVC unobtrusive validation won’t process them.

What you have to do is remove the validators and re-add them.

1
2
3
4
$(“#Password”).attr(“data-val-required”, “Password is required”);
$(“form”).removeData(“validator”);
$(“form”).removeData(“unobtrusiveValidation”);
$.validator.unobtrusive.parse(“form”);