maxlength attribute of a text box from the DataAnnotations StringLength in Asp.Net MVC

If you’re using unobtrusive validation, you can handle this client side as well:

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

Leave a Comment