TextBox.MultiLine maxlengthhttp://forums.asp.net/t/40065.aspx/1?TextBox+MultiLine+maxlengthWed, 12 Oct 2011 10:42:51 -04004006540065http://forums.asp.net/p/40065/40065.aspx/1?TextBox+MultiLine+maxlengthTextBox.MultiLine maxlength Is there some way of setting the MaxLength of a TextBox when it is in Mulitline TextMode? MSDN says the MaxLength doesn't work when in Multiline mode, but there has to be a way. I'm trying to make Textbox that allows two lines of input and no more. Ideally, it wouldn't let the user keep typing... Thanks. 2002-08-28T19:24:42-04:0040107http://forums.asp.net/p/40065/40107.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength For that you must have some script. I have one you need. If you mean to restrict user input on several characters! 2002-08-28T20:12:45-04:0040132http://forums.asp.net/p/40065/40132.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength Yes, I am trying to restrict the maximum number of characters to 60 in a MultiLine TextBox. 2002-08-28T20:40:41-04:00171998http://forums.asp.net/p/40065/171998.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength Has anyone found a solution to restricting the user's input in a multiline textbox??? I know that after the user has entered his/her information i can verify that there are the right amount of characters. The problem is that if i am wanting to keep the field down to 100 characters and they have just spend all of their time entering in 1000 characters, then it's been a complete waste of time for them. What I am trying to find is a way to restrict the length for a multiline textbox as in a singleline textbox; once the reach character 100....they can't enter any more information. does anyone have a solution to this???? 2003-03-14T14:47:08-05:00172135http://forums.asp.net/p/40065/172135.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength Hi, You can use this JavaScript code to control it ... <pre class="prettyprint">&lt;script language=&quot;javascript&quot;&gt; function Count(text,long) { var maxlength = new Number(long); // Change number to your max length. if (text.value.length &gt; maxlength){ text.value = text.value.substring(0,maxlength); alert(&quot; Only &quot; &#43; long &#43; &quot; chars&quot;); } }</pre> and use it in the text element's events onKeyUp=&quot;Count(this,200)&quot; onChange=&quot;Count(this,200)&quot; 2003-03-14T17:17:39-05:00172161http://forums.asp.net/p/40065/172161.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength BEAUTIMUS!!!! thank you very much 2003-03-14T17:48:56-05:00172274http://forums.asp.net/p/40065/172274.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength Nice Code! 2003-03-14T20:31:47-05:00475635http://forums.asp.net/p/40065/475635.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength Can anybody advise why code from Guille.Net cannot work on Mozilla? It would be great if somebody can suggest a solution that can work with Mozilla/Netscape. Thanks. Henry 2004-02-16T04:04:06-05:00475644http://forums.asp.net/p/40065/475644.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength I'm not sure what the problem under Mozilla is, but it might deal with the Number object. I really don't understand the point of using it.<pre class="prettyprint">&lt;script language=&quot;javascript&quot;&gt; function Count ( text, maxlength ) { if ( text.value.length &gt; maxlength ) { text.value = text.value.substring(0, maxlength); alert(&quot;Only &quot; &#43; maxlength &#43; &quot; chars&quot;); } } &lt;/script&gt;</pre> 2004-02-16T04:26:00-05:00475693http://forums.asp.net/p/40065/475693.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength when I have HTML text area then this code won't work. I spent lots of time fighting for this problem then came up with getElementbyID does anyone have some other solution? thankx mp417 2004-02-16T06:37:43-05:00475958http://forums.asp.net/p/40065/475958.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength How are you calling it? 2004-02-16T13:05:29-05:001844648http://forums.asp.net/p/40065/1844648.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength <font color="#0000ff" size="2"> <p>This is the code i think you may find usefull..Just pass the object to function from onkeydown as follows (<font color="#ff0000" size="2">onkeydown</font><font color="#0000ff" size="2">=&quot;javascript:checkLength(this);&quot;) and funtion will do the rest.It'll stop user entering any characters except del,backspace and arrraws to move left right and so on..</font></p> <p><font color="#0000ff" size="2"></font>&nbsp;</p> <p><font color="#0000ff" size="2">DONT FORGET to change the number (11000) with your Max characters value..</p> </font>function</font><font size="2"> checkLength(oObject)</font><font size="2"> <p>{</p> </font><font color="#0000ff" size="2">if</font><font size="2"> (oObject.value.length&lt;11000)</font><font size="2"> <p></font><font color="#0000ff" size="2">return</font><font size="2"> </font><font color="#0000ff" size="2">true</font><font size="2">;</p> </font><font color="#0000ff" size="2">else</font><font size="2"> </font><font size="2"> <p>{ </p> </font><font color="#0000ff" size="2">if</font><font size="2"> ((</font><font color="#0000ff" size="2">event</font><font size="2">.keyCode&gt;=37 &amp;&amp; </font><font color="#0000ff" size="2">event</font><font size="2">.keyCode&lt;=40) || (</font><font color="#0000ff" size="2">event</font><font size="2">.keyCode==8) || (</font><font color="#0000ff" size="2">event</font><font size="2">.keyCode==46))</font><font size="2"> <p></font><font color="#0000ff" size="2">event</font><font size="2">.returnValue = </font><font color="#0000ff" size="2">true</font><font size="2">;</p> <p></font><font color="#0000ff" size="2">else</p> </font><font size="2"></font><font color="#0000ff" size="2">event</font><font size="2">.returnValue = </font><font color="#0000ff" size="2">false</font><font size="2">; </font><font size="2"> <p>}</p> <p>}</p> </font> 2007-08-07T08:04:15-04:001897586http://forums.asp.net/p/40065/1897586.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength <p><font size="3"><font face="Times New Roman">Interesting thread, but for arguments sake what if I had a mandate&nbsp;for AA compliance and as such was trying to avoid js script like the plague. Is there a way of preventing more than a certain number of characters being entered into a multi line text box in aspx?</font></font></p> <p><font size="3"><font face="Times New Roman">(erm not only for the sake of argument :P )</font></font></p> 2007-09-07T10:22:39-04:001898034http://forums.asp.net/p/40065/1898034.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength <p>Hi, </p> <p>Setting the Maxength of a TextBox when it is in Multiline, You can use <a class="" title="RegularExpressionValidator control " href="http://www.vkinfotek.com/validationcontrols.html"> RegularExpressionValidator control</a> as shown below&nbsp;</p> <p>&lt;asp:TextBox ID=&quot;txtConclusion&quot; MaxLength=&quot;200&quot; TextMode=&quot;MultiLine&quot; Height=&quot;100px&quot; Width=&quot;400px&quot; runat=&quot;server&quot; /&gt;<br> &lt;asp:RegularExpressionValidator ID=&quot;txtConclusionValidator1&quot; ControlToValidate=&quot;txtConclusion&quot; Text=&quot;Exceeding 200 characters&quot; ValidationExpression=&quot;^[\s\S]{0,200}&#36;&quot; runat=&quot;server&quot; /&gt;</p> 2007-09-07T14:39:09-04:001900844http://forums.asp.net/p/40065/1900844.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength <p>&nbsp;</p> <p><font face="Times New Roman" size="3">Hi thanks for that.</font></p> <p>I am using r<font color="#a31515" size="2">equiredFieldValidator's on the rest of the page. The difference between what I was looking for and the validator is that, a validator is retro active. It produces an alert after the infraction as been comitted in this case forcing the user to reduce the number of charactures in the box. What i was hoping to do (without js) was to produce something that would stop a user from entering more than 200 charactures physcially into the text box in the first place, as you can do with a single line text box. </font></p> <p><font color="#a31515" size="2">&nbsp;Sorry for not being clearer the first time round.</font></p> <p><font color="#a31515"></font>&nbsp;</p> <p><font color="#a31515"></font><font color="#a31515" size="2">&nbsp;</p> </font> 2007-09-10T13:19:09-04:001902268http://forums.asp.net/p/40065/1902268.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength <p>hi there,</p> <p>&nbsp;the immediate solution for your problem wud be write a condition code for keypress event.</p> <p>&nbsp;here we go</p> <p>keypress event()</p> <p>{</p> <p>if (textbox1.textlength&gt;200) //textbox controll name is assumed to be textbox1</p> <p>{</p> <p>textbox1.enabled = false;</p> <p>messagebox.show(&quot;reached the limit&quot;);</p> <p>}</p> <p>}//keypressevent</p> <p>kindly check out the syntax specially the event name i have just named it on my own.</p> <p>&nbsp;This solution may have an impact on performance issue as this event will be triggered for every 200 key pressed.</p> 2007-09-11T07:41:37-04:001902491http://forums.asp.net/p/40065/1902491.aspx/1?Re+TextBox+MultiLine+maxlength+avoiding+Javascript+Re: TextBox.MultiLine maxlength avoiding Javascript! <p>&nbsp;I know there are many ways to do it, but if you want to avoid javascript completely, i would suggest creating a new control. Visit the following link for an indepth explanation as well as sample code!</p> <p>http://www.codeproject.com/useritems/Extended_ASPNET_TextBox.asp</p> <p>Just in case if the page being offline or taken away, I am also posting a screenshot of the page.&nbsp;</p> <p>&nbsp;</p> <p><span id="intelliTXT">&lt;div id=&quot;contentdiv&quot;&gt;</p> <h2>Introduction </h2> <p>Developers use multiline TextBox controls in almost all web projects. Since MaxLength property of a TextBox control does not work when the TextMode property is set to Multiline, we usually use Validator controls to validate the length. In this hands-on article, we are going to extend the TextBox control using JavaScript in order to limit the number of characters entered by the user to the length specified. </p> <h2>Using the code</h2> &lt;div class=&quot;smallText&quot; id=&quot;premain0&quot; style=&quot;width: 100%;&quot;&gt;<img src="http://www.codeproject.com/images/minus.gif" id="preimg0" height="9" width="9"><span id="precollapse0" style="margin-bottom:0pt"> Collapse</span>&lt;/div&gt; <pre id="pre0" lang="cs" style="margin-top:0pt">using System;<br>using System.Collections.Generic;<br>using System.ComponentModel;<br>using System.Text;<br>using System.Web;<br>using System.Web.UI;<br>using System.Web.UI.WebControls;<br>using System.IO;<br>using System.Globalization;<br>using System.Threading;<br><br>namespace CustomServerControls<br>{<br> [DefaultProperty(<span class="cpp-string">&quot;Text&quot;</span>)]<br> [ToolboxData(<span class="cpp-string">&quot;&lt;{0}:TextArea runat=server&gt;&lt;/{0}:TextArea&gt;&quot;</span>)]<br> public class TextArea : TextBox<br> {<br> public override TextBoxMode TextMode<br> {<br> get<br> {<br> return TextBoxMode.MultiLine;<br> }<br> }<br><br> protected override void OnPreRender(EventArgs e)<br> {<br> if (MaxLength &gt; 0)<br> {<br> if (!Page.ClientScript.IsClientScriptIncludeRegistered(<span class="cpp-string">&quot;TextArea&quot;</span>))<br> {<br> Page.ClientScript.RegisterClientScriptInclude(<span class="cpp-string">&quot;TextArea&quot;</span>, ResolveClientUrl(<span class="cpp-string">&quot;~/textarea.js&quot;</span>));<br> }<br> this.Attributes.Add(<span class="cpp-string">&quot;onkeypress&quot;</span>, <span class="cpp-string">&quot;LimitInput(this)&quot;</span>);<br> this.Attributes.Add(<span class="cpp-string">&quot;onbeforepaste&quot;</span>, <span class="cpp-string">&quot;doBeforePaste(this)&quot;</span>);<br> this.Attributes.Add(<span class="cpp-string">&quot;onpaste&quot;</span>, <span class="cpp-string">&quot;doPaste(this)&quot;</span>);<br> this.Attributes.Add(<span class="cpp-string">&quot;onmousemove&quot;</span>, <span class="cpp-string">&quot;LimitInput(this)&quot;</span>);<br> this.Attributes.Add(<span class="cpp-string">&quot;maxLength&quot;</span>, this.MaxLength.ToString());<br> }<br> base.OnPreRender(e);<br> }<br> }<br>}<br> <br></pre> <p>The code above creates a new TextArea custom server control by extending ASP.NET's TextBox control. By overriding the <strong>OnPreRender</strong> function, we include attributes to the HTML of the control. We add custom javascripts and a property to pass maxLength on client side.<br> <br> To show the working TextArea control, I prepared the following html:</p> <pre lang="aspnet">&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;Default.aspx.cs&quot; Inherits=&quot;_Default&quot; %&gt;<br>&lt;%@ Register TagPrefix=&quot;csc&quot; Namespace=&quot;CustomServerControls&quot; %&gt; <br><br>&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;<br>&lt;head runat=&quot;server&quot;&gt;<br> &lt;title&gt;TextArea Custom Server Control with MaxLength property&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br> &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;<br> &lt;div&gt;<br> &lt;csc:TextArea id=&quot;TextArea&quot; runat=&quot;server&quot; MaxLength=&quot;105&quot; Rows=&quot;10&quot; Width=&quot;300px&quot;&gt;&lt;/csc:TextArea&gt;<br> &lt;/div&gt;<br> &lt;/form&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br></pre> <p>In the above HTML, I register the custom control with the web page by using the following line: </p> <pre lang="aspnet">&lt;%@ Register TagPrefix=&quot;csc&quot; Namespace=&quot;CustomServerControls&quot; %&gt;</pre> <p>If you don't want to add the above registration line on each page that you use the TextArea control, you may add the following statement in system.web section of the web.config file. </p> <pre lang="aspnet">&lt;pages&gt;<br> &lt;controls&gt;<br> &lt;add tagPrefix=&quot;csc&quot; namespace=&quot;CustomServerControls&quot;&gt;&lt;/add&gt;<br> &lt;/controls&gt;<br>&lt;/pages&gt; </pre> I added the control on page as: <pre lang="aspnet">&lt;csc:TextArea id=&quot;TextArea&quot; runat=&quot;server&quot; MaxLength=&quot;105&quot; Rows=&quot;10&quot; Width=&quot;300px&quot;&gt;&lt;/csc:TextArea&gt;</pre> Let's compare the rendered output of a multiline textbox control and our text area control:<br> <br> Rendered output of a standard multiple line Asp.Net TextBox is:<br> <pre lang="aspnet">&lt;pages&gt;<br> &lt;controls&gt;<br> &lt;add tagPrefix=&quot;csc&quot; namespace=&quot;CustomServerControls&quot;&gt;&lt;/add&gt;<br> &lt;/controls&gt;<br>&lt;/pages&gt; </pre> Rendered output of a standard multiple line Asp.Net TextBox is:<br> <pre lang="aspnet">&lt;textarea name=&quot;TextArea&quot; id=&quot;TextArea&quot; rows=&quot;10&quot; cols=&quot;20&quot; style=&quot;width:300px;&quot; &gt;&lt;/textarea&gt; <br></pre> <p>Rendered output of our TextArea custom server control is:</p> <pre lang="aspnet">&lt;textarea name=&quot;TextArea&quot; rows=&quot;10&quot; cols=&quot;20&quot; id=&quot;TextArea&quot;<br>onkeypress=&quot;LimitInput(this)&quot; onbeforepaste=&quot;doBeforePaste(this)&quot;<br>onpaste=&quot;doPaste(this)&quot; onmousemove=&quot;LimitInput(this)&quot; maxLength=&quot;105&quot;<br>style=&quot;width:300px;&quot;&gt;&lt;/textarea&gt;<br></pre> <p>The javascript event handlers <strong>doBeforePaste</strong> and <strong>doPaste</strong> are only implemented in <strong>Internet Explorer</strong>. These event handlers are used to check the length of characters that are pasted by using a mouse in Internet Explorer. Unfortunately, <strong>doBeforePaste</strong> and <strong>doPaste</strong> event handlers are not defined in other browsers and we cannot catch a mouse paste in browsers other than IE. Therefore, I added an <strong>onmousemove</strong> event handler in order to check the length of characters that are pasted by using a mouse after a mouse move. The <strong>onkeypress</strong> event handler handles the standard character input. </p> <pre lang="aspnet">function doBeforePaste(control)<br>{<br> maxLength = control.attributes[&quot;maxLength&quot;].value;<br> if(maxLength)<br> {<br> event.returnValue = false;<br> }<br>}<br>function doPaste(control)<br>{<br> maxLength = control.attributes[&quot;maxLength&quot;].value;<br> value = control.value;<br> if(maxLength){<br> event.returnValue = false;<br> maxLength = parseInt(maxLength);<br> var o = control.document.selection.createRange();<br> var iInsertLength = maxLength - value.length &#43; o.text.length;<br> var sData = window.clipboardData.getData(&quot;Text&quot;).substr(0,iInsertLength);<br> o.text = sData;<br> }<br>}<br>function LimitInput(control)<br>{<br> if(control.value.length &gt; control.attributes[&quot;maxLength&quot;].value)<br> {<br> control.value = control.value.substring(0,control.attributes[&quot;maxLength&quot;].value);<br> }<br>}; </pre> <p>The code is tested on Firefox, IE 6.0 and IE 7.0. </p> <p><a href="http://www.bloggingdeveloper.com/posts/aspnet/textarea/default.aspx" target="_blank" title="Extended ASP.Net Multline Textbox with MaxLength Property">Try the online demo of extended Asp.net multiline TextBox control that limits the number of characters entered.</a> <br> <a href="http://www.bloggingdeveloper.com/" target="_blank" title="blogging developer - tips &amp; tricks on web development, search engine optimization and online marketing"><br> You may find more articles about Asp.net, JavaScript, C# on my blog.</a> </p> &lt;/div&gt; </span>&lt;script src=&quot;http://www.codeproject.com/script/togglePre.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt; <p></p> <h2>About blogging developer</h2> &lt;div style=&quot;overflow: hidden;&quot;&gt; <table border="0"> <tbody> <tr valign="top"> <td class="smallText" nowrap="nowrap"><br> </td> <td class="smallText">Blogging Developer is an accomplished project manager with more than 8 years of experience in web development, web strategy, e-commerce, and search engine optimization, specialized in object-oriented, multi-tiered design and analysis with hands-on experience in the complete life cycle of the implementation process including requirements analysis, prototyping, proof of concept, database design, interface implementation, testing, and maintenance.<br> <br> Solid project management skills, expertise in leading and mentoring individuals to maximize levels of productivity, while forming cohesive team environments. <p class="smallText">Click <a href="http://www.codeproject.com/script/profile/whos_who.asp?vt=arts&amp;id=4470396"> here</a> to view blogging developer's online profile.</p> </td> </tr> </tbody> </table> &lt;/div&gt; <p>&nbsp;</p> 2007-09-11T10:47:24-04:001902583http://forums.asp.net/p/40065/1902583.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength <p>best way to do it is create / develop a user defined textbox controll with a new property called maxlength and i think you could solve your problem</p> 2007-09-11T11:32:37-04:002346039http://forums.asp.net/p/40065/2346039.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength <p>Hello, <a href="http://forums.asp.net/members/vbsqlUser.aspx">vbsqlUser</a> version is nice, but I added some additionnal property to get that works.</p> <p>&nbsp;&nbsp; <br> SetFocusOnError=&quot;true&quot; EnableClientScript=&quot;true&quot; </p> <p>&nbsp;Like that when the user click on a runat server button, the button code isn't executed. Really interesting for what I need to do :)</p> <p>&nbsp;Maybe it could helps someone else. It works on Firefox and IE very well.<br> &nbsp;</p> 2008-05-08T15:35:07-04:002744059http://forums.asp.net/p/40065/2744059.aspx/1?Re+TextBox+MultiLine+maxlengthRe: TextBox.MultiLine maxlength <p>Here is modified version of RealCodeMaster's code to make it work in both IE and Firefox...</p> &nbsp;<pre class="prettyprint">function checkLength(e, oObject, maxLength) { if (oObject.value.length &lt; maxLength) return true; else { var keyID = (window.event) ? event.keyCode : e.keyCode; if ((keyID&gt;=37 &amp;&amp; keyID&lt;=40) || (keyID==8) || (keyID==46)) { if(window.event) e.returnValue = true; } else { if(window.event) e.returnValue = false; else e.preventDefault(); } } }</pre>&nbsp;Happy programming.<br> 2008-11-13T19:17:50-05:00