I'm trying to implement a custom validation attribute with client-side validation too.
My attribute looks like:
public class FileSize : ValidationAttribute, IClientValidatable { private readonly int _size; public FileSize(int size) { ErrorMessage = "Invalid size."; _size = size; } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ValidationType = "fileSize", ErrorMessage = ErrorMessage }; rule.ValidationParameters["size"] = _size; yield return rule; } public override bool IsValid(object value) { return ((HttpPostedFileBase) value).ContentLength < _size; } }
and the script includes in my view looks like:
<script src="@Url.Content("~/Scripts/jquery-1.4.4.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> <script type="text/javascript"> $(function () { $.validator.unobtrusive.adapters.addSingleVal('fileSize', 'size'); }); </script>
The problem is that even with all of those script includes and the function that registers a new adapter, then the client-side validation is still not working, it simply just keep using the server-side validation only...
Any ideas?