...

Maxtext v1.2 (cross-browser textarea maxlength script)

whammy
07-18-2002, 01:04 AM
<script language="javascript" type="text/javascript">
<!--
/*********************
Maxtext v1.2
© 2002 Robert K. Davis
**********************/
function textlen(x,y){
var thelength = x.value.length;
window.status=thelength+' of '+y+' maximum characters.';
}

function maxtext(x,y){
tempstr = x.value
if(tempstr.length>y){
x.value = tempstr.substring(0,y);
}
textlen(x,y);
}
/*********************/
// -->
</script>

<form name="myform">
<textarea name="mytextarea"
cols="45"
rows="3"
wrap="virtual"
onkeypress="return(this.value.length<20)"
onkeydown="textlen(this,20)"
onkeyup="textlen(this,20)"
onblur="maxtext(this,20)"
></textarea>
</form>


I know that's a lot of onkeystuff ;)

The ideal solution would be not to use a script and just use:

onkeypress="return(this.value.length<20)"

However, that only works in IE and Netscape - Opera completely ignores it.

To keep it simple and work in all browsers (at least the ones I have, that is!), that was the easiest solution I could come up with. This script was tested in Opera 6, Netscape 4.x, IE 4-6 on PC, and IE 4(I think) on MAC. If you have the opportunity to test it out in another browser, please let me know the results (good or bad).

In Opera, it will not limit the textarea until the onblur() kicks in. In Opera AND Netscape, you can paste past the maxlength of the textarea, which is corrected when the onblur() kicks in.

Every single other script of its type that I've tried will produce unexpected and sometimes very amusing results (like typing backwards) in those browsers.

bbdoc
02-23-2005, 10:52 AM
Hi,

Thanks for this script... this looks very nice and I could test it successfully :thumbsup:

Howewer, how is it possible to:
- Stop the user from typing when he has reached the maxsize for the textarea?? In the test I made, the user can continue typing, even if he is warned that the maxsize has been reached...
- Pop-up a message to the user when he tries to write more than the maxsize...

Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum