0

I have a textBox field with a MaxLength set to 255. I can enter more than 255 characters in the textbox. I was expecting there to be a 255 character limit.Is there any way to control this? Do I need to use the code behind? Can we control this with a RequiredFieldValidator?

here is the code in the .aspx

 <asp:TextBox ID="txtCommentaire" runat="server" Width="600px" MaxLength="255" TextMode="MultiLine" 
 Height="90px" style="overflow:hidden" ></asp:TextBox>
Liath
  • 9,913
  • 9
  • 51
  • 81
user3127986
  • 388
  • 1
  • 10
  • 33
  • You can use a `CustomValidator` or `RegularExpressionValidator`. Additionally you can use a javascript method that restricts the number of characters. – Tim Schmelter Jan 31 '14 at 15:12
  • http://www.codegod.com/aspnet-textbox-maxlength-in-multiline-mode-AID297.aspx – Steve Jan 31 '14 at 15:14

2 Answers2

5

MaxLength doesn't apply when you have TextMode="Multiline".

when you use <asp:TextBox TextMode="Multiline">, a <textarea> is rendered to the page. TextArea tags didn't support a maxlength attribute until recently (html5). Therefore, when this feature was initially designed, Microsoft ignored the MaxLength attribute in Multiline mode.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
Aheho
  • 12,622
  • 13
  • 54
  • 83
2

The MaxLength property does not function while the TextBox is in multi-line mode. This needs to be made single line or needs a different enforcement technique (like using javascript to hook a key press event)

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454