Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

limit textarea including line returns

limit max characters in a TEXTAREA but accounting for returns

1:19 pm on Dec 5, 2003 (gmt 0)

New User

10+ Year Member

joined:Dec 4, 2003
votes: 0

ok, im using this javascript to limit the number of characters in a 'TEXTAREA'

function TrackCount(fieldObj,countFieldName,maxChars)
etc.. sorry i could not post it here, it cut cutting it off half way!

that works fine so far, it limits the number of characters no problem, but the problem is it does not take into account line returns,

i.e. the textarea i am using is roughly 30 characters wide, and i need the script to take this into account. i am building a ecard system and there is limited space on the card for the message text, it all works fine until the user uses line returns. these need to be counted as if the user had typed in '30' characters. does that make any sense?

im not much good in javascript so please help if you can


7:39 am on Dec 8, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 21, 2002
votes: 0

these need to be counted as if the user had typed in '30' characters.

If the user types halfway across and then puts in a newline, the newline only counts for 15 characters.

What you're after can be done:

You want to find the position of the first newline char in the string like this:

var myPosition = myString.indexOf("\n")

You have to do some maths to work out how many to add to the char count (e.g. if it's in position 42 you add 18).

You then have to chop of the first part of the string (including the newline char) and search for a newline again in case there's more than one (and do the maths again). Repeat until there are no more newlines.