srkirkland / Html5MvcTemplates

Html5 ASP.NET MVC Input Templates

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding min and max attributes to Number template based on Range DataAnnotation

mikecole opened this issue · comments

As far as I know there isn't a way to get validation annotations set on a property from inside of the Editor Templates, so adding MaxLenth or Min/Max attributes would seem to be impossible by this method without some other custom process. Probably using the built in jquery validation stuff would be your best bet, as nice as this would be to have.

What are your thoughts about a supplemental unobtrusive jQuery file that adds these bell and whistles, pulling from the validation values that MVC creates?

Something like so:

$("input[data-val-range-max]").each(function () {
    var value = $(this).data("val-range-max");
    $(this).attr("max", value);
});

$("input[data-val-range-min]").each(function () {
    var value = $(this).data("val-range-maxmin");
    $(this).attr("min", value);
});

$("input[data-val-length-max]").each(function () {
    var value = $(this).data("val-length-max");
    $(this).attr("maxlength", value);
});