Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying maxlength for multiline textbox

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?

like image 739
Blerta Avatar asked Aug 26 '09 12:08

Blerta


People also ask

How can set maximum length of TextBox in asp net?

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 .

What is the used of the MaxLength property in a TextBox?

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.

What is multiline in TextBox?

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.

What is the max length of TextBox?

The default value is 32767 .


1 Answers

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.

like image 132
Alex Angas Avatar answered Sep 28 '22 18:09

Alex Angas