Show message when user exceed limit size of a textbox in asp.net mvc app

Total Post:52

Points:366
Mvc4 
C#
 611  View(s)
Ratings:
Rate this:

Show message when user exceed limit size of a textbox in asp.net mvc app

I am developing a mvc application in that I want to display a message when the user types more than 20 characters in the textbox. This needs to validate without clicking on any validate button.

This is my current code :

<span class="field">@Html.TextBoxFor(d => d.Name,new {maxlength=20})</span>
  1. Post:45

    Points:323
    Re: Show message when user exceed limit size of a textbox in asp.net mvc app

    Use this code

    In the Model

    [StringLength(250, ErrorMessage = "Description must not be more than 250 chars")]
    
            public string Name{ get; set; }
    View code
    HtmlHelper.ClientValidationEnabled = true;
    
      HtmlHelper.UnobtrusiveJavaScriptEnabled = true;

    <div class="col-md-12">
                                    <div class="form-group">

                                        @Html.TextAreaFor(m => m.Name, new { @class = "form-control"})
                                        @Html.ValidationMessageFor(x=>x.Name)
                                    </div>
                                </div>

Answer