Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
2.7k views
in .Net Framework by 5 5 8

Is it possible to limit the number of rows for a Multiline Textbox to only show 5 rows with a total character of 200 characters per textbox in ASP.NET? If YES, How can I set minimum and maximum length in ASP.NET TextBox?


1 Answer

1 like 0 dislike
by 159 200 385
selected by
 
Best answer

How to set the number of ROWS for a Multiline TextBox in ASP.NET?

You can easily use the ROWS attribute in Multiline TextBox in ASP.NET to just limit the DISPLAYED number of ROWS.

<asp:TextBox class="form-control" ID="txt_messageBody" runat="server" TextMode="MultiLine" Rows="5"></asp:TextBox>

How to limit the character numbers for a Multiline TextBox in ASP.NET?

The above solution will limit the displayed number of rows in Multiline TextBox, but it will not limit the MAX number of characters.
To set the Minimum and Maximum number of characters in MultiLine TextBox in ASP.NET, you can use asp:RegularExpressionValidator as below

<asp:TextBox class="form-control" ID="txt_messageBody" runat="server" TextMode="MultiLine" Rows="5"></asp:TextBox>
<asp:RegularExpressionValidator Display ="Dynamic" ControlToValidate ="txt_messageBody" ID="RegularExpressionValidator1" ValidationExpression = "^[\s\S]{50,200}$" runat="server" ErrorMessage="You should provide at least 50 charachters (Maximum 200 characters)" ValidationGroup="contactForm"></asp:RegularExpressionValidator>

For Single line TextBox, you can use MaxLength attribute as below:

<asp:TextBox ID="TextBox1" MaxLength="200" runat="server"/>
If you don’t ask, the answer is always NO!
...