On client I create some inputs, for example:
var title = document.createElement('input');
title.setAttribute('type', 'text');
title.setAttribute('name', 'Title');
myForm.appendChild(title);
This field should be required. How can I do this validation?
You should apply HTML5
data-*attributes to it that are used by the jquery unobtrusive validation and then calljQuery.validator.unobtrusive.parseto force the plugin reparse the rules that have been dynamically added:Also notice that you might want to provide a corresponding
<span>element that will be used to display the error message unless you are using theHtml.ValidationSummaryhelper to centralize all error messages in a single location. This element should look like this:which could be written in a more jQueryish way: