I wrote a simple custom validator. It works on form submission, but not real-time on the client side like I think it should. Is this code correct? Is there something I need to do on the View? For the record, the other (built-in) validators work on the client side. I even added
@{
Html.EnableClientValidation(true);
ViewBag.Title = "CaseVue | Patient";
}
to the top of the view. Here's my validator code:
public class BirthDateRangeAttribute : ValidationAttribute, IClientValidatable
{
public int MaxAge { get; set; }
public BirthDateRangeAttribute()
{
}
public override bool IsValid(object value)
{
string val = value.ToString();
if (!string.IsNullOrWhiteSpace(val))
{
DateTime dt;
if (DateTime.TryParse(val, out dt))
{
return dt >= DateTime.Now.AddYears(MaxAge * -1) && dt <= DateTime.Now;
}
else
{
return false;
}
}
return false;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule rule = new ModelClientValidationRule {
ErrorMessage = this.ErrorMessage,
ValidationType = "birthdaterange"
};
rule.ValidationParameters.Add("maxage", this.MaxAge);
yield return rule;
}
}
Anyone see what I'm doing wrong?