MaxLength Attribute not generating client-side validation attributes

Try using the [StringLength] attribute: [Required(ErrorMessage = “Name is required.”)] [StringLength(40, ErrorMessage = “Name cannot be longer than 40 characters.”)] public string Name { get; set; } That’s for validation purposes. If you want to set for example the maxlength attribute on the input you could write a custom data annotations metadata provider as shown … Read more

ViewModel validation for a List

If you are using Data Annotations to perform validation you might need a custom attribute: public class EnsureOneElementAttribute : ValidationAttribute { public override bool IsValid(object value) { var list = value as IList; if (list != null) { return list.Count > 0; } return false; } } and then: [EnsureOneElement(ErrorMessage = “At least a person … Read more

MVC model validation for date

There is no need to disable jQuery date validation (and that is likely to cause other issues). You just need to override the range method of the $.validator. By default, it works with numeric values (and then falls back to a string comparison), so you can add the following script (after jquery.validate.js and jquery.validate.unobtrusive.js, but … Read more