This field is optionally validated based on its visibility. If the field is hidden then we don't want to validate it but if it's not hidden then I would want to add required validation.
I'm manually checking it now and stepping through the debugger I can see that the MS unobtrusive validation library is resetting the classes after I've changed the class attribute.
I'm guessing there's some built in methods I can call from the unobtrusive validation library but can't seem to get it figured out.
Model
[Column(TypeName = "varchar")]
[StringLength(150, ErrorMessage = "Url must be less than 151 characters.")]
public string Url { get; set; }
View Javascipt
if ($("#Url").val() === '') {
$('#Url').addClass('input-validation-error');
return false;
} else {
$('#Url').removeClass('input-validation-error');
}
View Form
<div>
@Html.TextBoxFor(m => m.Url)
@Html.ValidationMessageFor(m => m.Url)
</div>