I have several input separated into different containers (panels). The problem I have is that if one of these panels is hidden (style="display:none;"
), the jQuery.validate plugin, does not validate those inputs.
I make a test with a small example and happens the same problem:
view:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Fields</legend>
<div class="UserName" style="display:none;">
<div class="editor-label"> @Html.LabelFor(model => model.UserName) </div>
<div class="editor-field">
@Html.TextBoxFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName)
</div>
</div>
<div class="editor-label"> @Html.LabelFor(model => model.FirstName) </div>
<div class="editor-field">
@Html.TextBoxFor(model => model.FirstName) @Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.LastName) @Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label"> @Html.LabelFor(model => model.City) </div>
<div class="editor-field">
@Html.TextBoxFor(model => model.City) @Html.ValidationMessageFor(model => model.City)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Model:
public class UserModel {
[Required]
[StringLength(6, MinimumLength = 3)]
[Display(Name = "User Name")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
[ScaffoldColumn(false)]
public string UserName { get; set; }
[Required]
[StringLength(8, MinimumLength = 3)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[StringLength(9, MinimumLength = 2)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required()]
public string City { get; set; }
}
The "UserName" property is not validated on the client when is inside the div with style "display:none;"
Thank you