I'm trying to use asp:
<asp:TextBox ID="txtInput" runat="server" TextMode="MultiLine"></asp:TextBox>
I want a way to specify the maxlength
property, but apparently there's no way possible for a multiline textbox
. I've been trying to use some JavaScript for the onkeypress
event:
onkeypress="return textboxMultilineMaxNumber(this,maxlength)" function textboxMultilineMaxNumber(txt, maxLen) { try { if (txt.value.length > (maxLen - 1)) return false; } catch (e) { } return true; }
While working fine the problem with this JavaScript function is that after writing characters it doesn't allow you to delete and substitute any of them, that behavior is not desired.
Have you got any idea what could I possibly change in the above code to avoid that or any other ways to get round it?
Use the MaxLength property to limit the number of characters that can be entered in the TextBox control. This property is applicable only when the TextMode property is set to TextBoxMode. SingleLine or TextBoxMode. Password .
MaxLength property of TextBox is used to set maximum number of character that we can input into a TextBox. Limit of MaxLength property is 1 to 32767. By default this property is set to 32767.
A multiline text box control is a large text box that can display several lines of text or accept this text from user input. Text boxes are often linked to select value lookups and detailed menus. You can place a multiline text box control within a section control.
The default value is 32767 .
Use a regular expression validator instead. This will work on the client side using JavaScript, but also when JavaScript is disabled (as the length check will be performed on the server as well).
The following example checks that the entered value is between 0 and 100 characters long:
<asp:RegularExpressionValidator runat="server" ID="valInput" ControlToValidate="txtInput" ValidationExpression="^[\s\S]{0,100}$" ErrorMessage="Please enter a maximum of 100 characters" Display="Dynamic">*</asp:RegularExpressionValidator>
There are of course more complex regexs you can use to better suit your purposes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With