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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maximum length on a multiline textbox

Status
Not open for further replies.

sodakotahusker

Programmer
Mar 15, 2001
601
In the past I have gotten around the lack of a maximum length attribute on textarea elements by using a behavior

style="BEHAVIOR: url(maxlength.htc)" maxlength = "150"
inside my element.

That is not working with ASP.NET server side textbox - because Microsoft chose not to write out the maxlength attribute on the textarea - thinking that it was not functional. However it is functional with the behavior applied. The only recourse I see is to use the HTML element instead of the server side control - but I'd prefer not the mix the two types of elements. Anybody have any suggestions?

Anybody from Microsoft here to comment on this??

 
I might be missing something here - but it looks like all this does is check to length of the contents after the fact. I can certainly do that much with a little client script. That does not prevent the user from typing an entry the length of War and Peace into the textbox. I hate to tell someone that I am truncating their masterpiece after they spend half a day typing. I'd like to stop them as soon as they hit the limit. I suppose I could put in an event to check the length after every keystroke (which I guess basically is what the htc does). The HTC just made it seem very easy to implement.

I have written the client script:

<script language =jscript>
function checkLen(e,maxlen)
{
var realMax = maxlen -1;
if (e.value.length > realMax)
{

alert("You have reached the maximum number of characters which is " + maxlen + ". Any added characters will be discarded!");
if (e.value.length > maxlen)
e.value = e.value.substr(0,realMax)
return false;
}

}
</script>

then in the html for the asp:textbox control I put

onkeypress="return checkLen(this,12)"

max length is enforced and keystroke is discarded.

Thanks for getting me to think of the box. I prefer the other solution - but this will work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top