Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > TextBox's MaxLength does not work when TextMode=MultiLine

Reply
Thread Tools

TextBox's MaxLength does not work when TextMode=MultiLine

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      09-04-2005
When using a MultiLine TextBox (which generates a TextArea) setting the
MaxLength property has no effect. However, because I am saving the contents
of the TextBox to a DB field, I must limit the number characters allowed. I
would prefer not to do this serverside because that would require a large
amount of trips between the server and client (especially for a 200
character limit). Is there a way to do this? Thanks.
--
Nathan Sokalski
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
Jordan
Guest
Posts: n/a
 
      09-04-2005
Single line textboxes render as an HTML <input> tag - which supports a max
length.

The Multiline Textbox, renders as an HTML <TextArea> which doesn't support a
length property.

So, one workaround is to roll your own by calling a JavaScript function that
checks the length of the text entered. The function gets called on every
keypress and denies further data entry when the max length is reached. Of
course the JavaScript function needs to ignore some keys. This one should
work. It limits to 300 characters:

This is how your multiline text box will render.
<textarea name="MyTextArea" rows="3" id="MyTextArea" onkeypress="return
checkMaxLength(event,this)" TAMaxLength="300"
style="width:350px;"></textarea>

Here's your JavaScript function:
function checkMaxLength(e,el) {
switch(e.keyCode) {
case 37: // left
return true;
case 38: // up
return true;
case 39: // right
return true;
case 40: // down
return true;
case 8: // backspace
return true;
case 46: // delete
return true;
case 27: // escape
el.value='';
return true;
}
return (el.value.length<el.getAttribute("TAMaxLength"));
}

-HTH

"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> When using a MultiLine TextBox (which generates a TextArea) setting the
> MaxLength property has no effect. However, because I am saving the
> contents of the TextBox to a DB field, I must limit the number characters
> allowed. I would prefer not to do this serverside because that would
> require a large amount of trips between the server and client (especially
> for a 200 character limit). Is there a way to do this? Thanks.
> --
> Nathan Sokalski
> (E-Mail Removed)
> http://www.nathansokalski.com/
>



 
Reply With Quote
 
 
 
 
S. Justin Gengo
Guest
Posts: n/a
 
      09-04-2005
Nathan,

While Jordan's solution is certainly a viable one I prefer to use a regular
expression validator with the following regular expression in it:
^[\s\S]{0,25}$

Change the "25" to whatever the max character length allowed is.

This type of validator validates client side if possible, but also validates
server side so that no-one can bypass the check.

It also works even if someone has turned off javascript.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Jordan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Single line textboxes render as an HTML <input> tag - which supports a max
> length.
>
> The Multiline Textbox, renders as an HTML <TextArea> which doesn't support
> a length property.
>
> So, one workaround is to roll your own by calling a JavaScript function
> that checks the length of the text entered. The function gets called on
> every keypress and denies further data entry when the max length is
> reached. Of course the JavaScript function needs to ignore some keys. This
> one should work. It limits to 300 characters:
>
> This is how your multiline text box will render.
> <textarea name="MyTextArea" rows="3" id="MyTextArea" onkeypress="return
> checkMaxLength(event,this)" TAMaxLength="300"
> style="width:350px;"></textarea>
>
> Here's your JavaScript function:
> function checkMaxLength(e,el) {
> switch(e.keyCode) {
> case 37: // left
> return true;
> case 38: // up
> return true;
> case 39: // right
> return true;
> case 40: // down
> return true;
> case 8: // backspace
> return true;
> case 46: // delete
> return true;
> case 27: // escape
> el.value='';
> return true;
> }
> return (el.value.length<el.getAttribute("TAMaxLength"));
> }
>
> -HTH
>
> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> When using a MultiLine TextBox (which generates a TextArea) setting the
>> MaxLength property has no effect. However, because I am saving the
>> contents of the TextBox to a DB field, I must limit the number characters
>> allowed. I would prefer not to do this serverside because that would
>> require a large amount of trips between the server and client (especially
>> for a 200 character limit). Is there a way to do this? Thanks.
>> --
>> Nathan Sokalski
>> (E-Mail Removed)
>> http://www.nathansokalski.com/
>>

>
>



 
Reply With Quote
 
zed_rhodes zed_rhodes is offline
Junior Member
Join Date: May 2009
Posts: 1
 
      05-29-2009
I know this is an old thread but just wanted to point out something I discovered that may save others a bit of time.

Quote:
this also:- h t t p : / / w w w . d y n a m i c d r i v e . c o m / d y n a m i c i . . . / m a x l e n g t h . h t m
may not be the best option. The technique employed seems to be easily defeatable if you hold down a key and then, while still holding it down, click in another field. I was able to insert an arbitrary number of characters into the textarea "with a maxlength of 10" in this manner, which remained in the field after I set the focus to another field. It seems the user would have to be intentionally trying to break something - but with some users you never know.

I can't speak for other browsers, but for IE7 I've had better luck with the regular expression method.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why doesn't MaxLength work with multiline TextBoxes? danthman ASP .Net 11 05-11-2010 10:00 AM
MaxLength doesn't seem to work Samuel ASP .Net 1 09-11-2007 04:28 PM
TextBox's MaxLength does not work when TextMode=MultiLine Nathan Sokalski ASP .Net Web Controls 1 09-04-2005 07:29 PM
TextBox - MaxLength property =?Utf-8?B?bWc=?= ASP .Net 1 04-11-2004 02:29 PM
Textbox maxlength..... Why doesnt that ever work??? Darren Clark ASP .Net 1 04-10-2004 02:56 AM



Advertisments