Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you limit text in a multiline textbox? 3

Status
Not open for further replies.

VBRookie

Programmer
May 29, 2001
331
US
I know that the maxlength property is invalid for textboxes where the textmode is set to multi-line. Is there another way to set the maxlength?

- VB Rookie
 
VB: Have you thought of using a validator? Or did you want the typist to see explicitly that the maximum number of characters are exhuasted?
 
Well, I wanted it to work kind of how the single-line textboxes do where when they reach the limit no more characters are accepted. I think that I have it figured out. I registered this Javascript function in my code behind on the page load and then called it in the control:

Javascript Code
Code:
System.Text.StringBuilder jScriptStr2 = new System.Text.StringBuilder ();
			jScriptStr2.Append (&quot;<script language=javascript>&quot;);
			jScriptStr2.Append (&quot;function limitText (textObj, maxCharacters){&quot;);
			jScriptStr2.Append (&quot;if (textObj.innerText.length >= maxCharacters){&quot;);
			jScriptStr2.Append (&quot;if ((event.keyCode >= 32 && event.keyCode <= 126) || (event.keyCode >= 128 && event.keyCode <= 254)){&quot;);
			jScriptStr2.Append (&quot;event.returnValue = false;&quot;);
			jScriptStr2.Append (&quot;}}}</script>&quot;);
			Page.RegisterStartupScript (&quot;textLimiter&quot;, jScriptStr2.ToString ());


HTML code
Code:
<asp:TextBox TextMode=&quot;MultiLine&quot; OnKeyDown=&quot;limitText (this, 255);&quot; Rows=&quot;2&quot; Width=&quot;175px&quot; ID=&quot;txtDescription&quot; Runat=&quot;server&quot;/>

This creates the same behavior as the single line textbox. When the user hits 255 characters they can't type anymore. It seems to be working ok so far ... I haven't run into any issues with it.

Thanks for the suggestion though,
- VB Rookie
 
VB. I love your handle, something that would fit me - I'm new at all this. However, you deserved the star, not I!

Paul(link9) has recommended avoiding javascript when you can and when it is more practical to do so. However, its a powerful client side language and necessary at times.

Your routine is very nice, and as you said it seems to be working. If I run across another method, I'll repost here - I'll keep my eyes open.
 
Thanks man,

I will also keep an eye out for an improved - JavaScriptless way to do it.

- VB Rookie
 
isn't there a maxlength on textboxes???
<asp:TextBox id=&quot;txt_desc&quot; Runat=&quot;server&quot; Width=&quot;206px&quot; MaxLength=&quot;255&quot;></asp:TextBox>
 
You might have something there ws, cd be, not certain of that; will test it - have you tested that? Looks good.
 
I ran the following test on this:

I set the maxlength property of a multiline textbox to 5 and tried to type in it. I was able to go above and beyond 5 characters. That brings me to the conclusion that the maxlength property doesn't work when the textmode of the textbox is set to multiline. It works fine otherwise.

I ran the test again only this time instead of using the maxlength property I put in the javascript code that I referenced in one of the replies above. It worked beautifully.

wsmall73 how did you run your test? Maybe I can try what you did to get it to work and then I can dump the javascript altogether.

Thanks,
- VB Rookie
 
Dear
Use a function in the javascript with the keypress event for the multiline textbox where in count the length of the textbox matter and if it is greater that what is constrained then pop up the alert.


bye
 
I've made some script that takes care of the maxlength for textarea's
This takes care of pasting stuff into the box too
Set the pref and onkeyup of the textbox with attributes
pref is the current value of the textbox

Code:
<script language=javascript>
	function limitText (textObj, maxCharacters){
		if(textObj.value.length<maxCharacters){
			textObj.pref=textObj.value
		}else{
			textObj.value=textObj.pref;
		}
	}
</script>
<textarea onkeyup="return limitText(this,4)">

</textarea>


<textarea pref="" onkeyup="limitText(this,40)">
</textarea>



Greetings, Harm Meijer
 
I must be really lazy! I ususally just chop off whever they entered past what I warned them they could!

Code:
int maxChars = 4000;

string adjustedText;

if(this.tbTextArea.Text.Trim().Length > maxChars){
    adjustedText = this.tbTextArea.SubString(0, maxChars);
}else{
    adjustedText = this.tbTextArea.Text.Trim();
}

:)

Greetings,
Dragonwell
 
Well another option would be to put a regular expression validator on the control and then insert some regex code as the validation expression that limits the input to whatever length you determine.

I've tried this and it works even if a user trys to paste in a larger amount of characters than what is allowed.

- VB Rookie
 
Another nice option i usually add is a little textbox where the user can see how many character they still can type.

search on google for "free sms" and view the source of one of these millions of pages for examples.

Edward@de-leau.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top