TextBox's MaxLength does not work when TextMode=MultiLine

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

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.
 
J

Jordan

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
 
S

S. Justin Gengo

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
 
Joined
May 29, 2009
Messages
1
Reaction score
0
I know this is an old thread but just wanted to point out something I discovered that may save others a bit of time.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top