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

Limiting the size of a text area 4

Status
Not open for further replies.

jewel464g

Technical User
Jul 18, 2001
198
US
I need to limit the size of a text area. I've tried maxlength="800" but this doesn't work. Does anyone have any ideas on how to do this?
 
There is no property for that. You can do it with some server-side or client-site script. Do a keyword search in a forum for server-side scripts (if you use a script language)or in the javascript-forum for client-site.

You are not the first one with this question,
Erik
 
try this:

<html>
<head>
<title></title>

<script language=&quot;JavaScript&quot;>
var txtAreaChars = 0;
var notLimited = true;
var txtMax
var firstBit = &quot;&quot;;
function countArea()
{
txtMax = document.getElementById(&quot;txtArea&quot;).innerHTML;
if (txtAreaChars < 20)
{
txtAreaChars+= 1;

}
else
{
if (txtMax.length < 20)
{
notLimited = true;
txtAreaChars = txtMax.length + 1;
}
else
notLimited = false;
}
}
</script>
</head>
<body>
<textarea id=&quot;txtArea&quot; rows=5 cols=40 onKeyPress=&quot;countArea(); return notLimited&quot;>

</textarea>
</body>
</html>


should work just fine.

-pm
 
good-dy!!

here is another script (would work in non-DOM-browsers too :):

~~~~~~~~
Code:
<html><head><title>textarea length</title>
<SCRIPT language=&quot;JavaScript&quot;>
<!-- 
function count_change(form){
 Msg = form.txtarea.value;
 Msglen= Msg.length;
 //maximum length
 MaxLen=20;
 if (Msglen > MaxLen ){
 form.txtarea.value=Msg.substring(0,MaxLen);
alert(MaxLen+&quot; symbols allowed only!!&quot;)
} form.txtarea.focus(); }
//-->
</SCRIPT>
</head>
<BODY>
<FORM name=&quot;form1&quot; METHOD=&quot;POST&quot; >
<TEXTAREA cols=50 name=txtarea rows=5 MAXLENGTH=20 WRAP=virtual onChange=&quot;count_change(document.forms[0])&quot; onkeypress=&quot;count_change(document.forms[0])&quot;></TEXTAREA><br>
<input type=button value=&quot;show length&quot; onclick=&quot;alert(this.form.txtarea.value.length)&quot;>
</form>
</body>
</html>
[color]~~~~~~~~ Victor
 
Hi pm,

Is there a way to make it where people can't paste in over 20 characters? The code works great if people just type in but if they paste the characters in, then it doesn't work. Here's a link to the code, so you can see what I mean in action.


Thanks,

Jewel
 
i thought my script works nice.. well, may be not for you.. Victor
 
Hi phentalmyst: you did a good job, but vituz code is usable for a larger group of browser-users and also usable when users paste oversized data.

Hi vituz: I tested your code in IE4.0 and NN4.73 and it works PERFECT !!! I wish I had new it before !!!
6 months ago a made a terrible server-site ASP script for this test. That cost me a lot of reguest time !!!
I'm gona use it from now on. THANX !!! You own a star too !!

Hi jewel: It takes you only 1 minute to cut and paste vituz's code in your's and your problem about the &quot;oversized paste&quot; is solved. Vituz's answer was allready there before you posted your last question ???

Good luck you all,
Erik

 
im having that cut and paste problem too, i use script and also check the length before i submit the form to double check the length is valid
 
GREAT WORK VICTOR.

But i just test it on NE4.5 and after the script cut the longers string the last key pressed goes first in the string.


Hope this helps.
 
thanks juanferman,
yea, just noticed it (i do not use netscrap, (as most of us except iza, i guess :]]]);

when i tested it, i saw that it worked, but hadn't had a carefull look (escuse me for my english, if the last sentence was wrong :)

i've added one thing to go thru this accident:

<TEXTAREA .. onChange=&quot;return count_change(document.forms[0])&quot; onkeypress=&quot;return count_change(document.forms[0])&quot;></TEXTAREA>

use return except of just calling that function; & then, in the function itself, return true or false:

function count_change(form){
Msg = form.txtarea.value;
Msglen= Msg.length+1;
MaxLen=20;
if (Msglen > MaxLen){
form.txtarea.value=Msg.substr(0,MaxLen);
return false
}else{ return true }
}

i've also tryed to use RegExp object with it's $n properties, just replacing with expressions, storing every value that's length is less than maxValue, but of no avail.. what i found workable is just using return

i added +1, becouse when this function called onkeypress, the length is still previous (you know what i mean)

but now it won't let you edit this area when you're outta maxlength.. only using del key..
so, it's up to you, guyz (& young ladyz :]) what you're gonna do: support netscrap or say &quot;what'da %^*(^&& ? i just don't give a !@@#$%#%^$% if it doesn't work properly in netScrap!! it works & that's GOOD&quot; Victor
 
Hi juanferman, vituz,

Your original code works perfect in NN4.73 !!!! I don't notice juanferman's posted bug in NN4.73

Erik


 
Very Good job Victor.

Felicitaciones. Otra estrella.


Hope this helps.

Regards

Juan F. Sarria
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top