Question

I'm having problems with jquery validation showing the default required validation message.

I add these textboxes dynamically using javascript. And I assume some class is missing for it to trigger the "data-val-required" attribute. Could that be the case?

Result after inspect:

<input class="required input-validation-error" data-val="true" 
    data-val-required="Custom error message here" type="text" 
    value="" id="Application_EducationReferences_0__Course"      
    name="Application.EducationReferences[0].Course">

<span class="field-validation-error" 
    data-valmsg-for="Application.EducationReferences[0].Course" 
    data-valmsg-replace="true">
    <span for="Application_EducationReferences_0__Course" class="">
      This field is required.</span>
</span>
Was it helpful?

Solution

When you have client-side generated lists of input fields with unobtrusive validation you need to refresh, use this script to refresh the rules correctly:

$("form").data("unobtrusiveValidation", null);
$("form").data("validator", null);
$.validator.unobtrusive.parse($("form"));

this article describe that code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top