If you try to change the TextMode property of ASP.NET TextBox control to Multiline and set the MaxLength property it’ll not work and user can insert as much characters as he/she want. WHY? Because when you put TextBox in your WebForm it’ll be rendered to HTML <input> tag but when you set the TextMode to multiline it’ll be rendered to <textarea> tag, and MaxLength attribute is in <input> but it’s not for <textarea>.
So dude How can I solve this issue?
From my opinion it’s better to solve this by using JavaScript function than using RegularExpressionValidator, so let’s write the JavaScript function and call it in onKeyPress TextBox client event.
- function textboxMultilineMaxNumber(txt, maxLen) {
- if (txt.value.length > (maxLen – 1)) return false;
- else {
- return true;
- }
- }
- <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"
- onkeypress="return textboxMultilineMaxNumber(this,15);">
- </asp:TextBox>
As you can see in line number 2 I pass TextBox and MaxLength, this will make the max number of characters user can input in the MultiLine TextBox is 15.