Page 1 of 1

I need some help with a textarea

Posted: Thu Apr 24, 2003 11:23 pm
by gab
Hello,

I'm modifing a phpforum and would like to know if it's possible to make a textarea limited. This is, if the text get's to the end of the textarea box I don't want it to scroll down for the next line. Just stop there. I tried the javascript that limits the characters but it doesn't work because if the user press enter it counts just one char and it gets out of the textbox.

I hope you understand what I mening. Excuse mi english please

Thanks Gabriel

Posted: Thu Apr 24, 2003 11:35 pm
by patrikG
PHP won't do a thing with a <textarea> as PHP is serverside. What you are looking for is a Javascript solution.

Off the top of my head, have an "onChange='myFunctionCalledCountChars()'" in the <textarea> so that every time a key is pressed, Javascript knows how many were input already. Once the limit is reached, cancel the event.bubble and return false;

Posted: Thu Apr 24, 2003 11:49 pm
by gab
hello,

I'm sorry, I'm new in web prog.

I already used a javascript function- TrackCount

Is very similar to what you described.

But the problem is that the return key counts as one character. And i need to conserve the layout. Do you know if there's anyway to desable return on a textarea box?

thnx

\r

Posted: Fri Apr 25, 2003 3:15 pm
by phpScott
At least in Windows the textarea uses the \r for a new line so you can count how many \r are in the field and stop it once you reach the limit.

Know you have something more to learn ereg expressions or string searches.

If \r doen't work try \n or both togther \r\n It has been a while since I dealt with it but I seem to remeber having to do something similiar

Posted: Fri Apr 25, 2003 8:23 pm
by patrikG
I've tested the event-bubbling of keyboard-input and, using onKeyDown and onChange for <textarea> both cancelling the even-bubble and returning false couldn't stop that text was entered into a field after a certain condition (i.e. text-length of the textarea) was met.

I would suggest you do use PHP to determine the string-length and, if too long, have the user re-submit the form, i.e. shorten the text.

Alternatively, you could spew out a warning that the text-limit has been reached if the user is inputting further text.