Javascript/ Text Area Line Limits

Bes

Bes

Soldato
Joined
18 Oct 2002
Posts
7,318
Location
Melbourne
Hi

I am trying to make a bit of javascript to limit the number of lines of text that can be put in a text area. At the moment it does limit the number of lines, but it needs to take wrapping into account. (i.e. if 3 lines are permitted, and there are already 2 lines because one has wrapped, it needs to consider this).

I am basing it on a script I found on the 'net.

To account for the wrapping, I propose having the JS insert a linebreak after a certain number of characters (I can write a regex to do this possibly), but the problem is that I don't know how to reference the following lines object(?)

The code is something like this:

PHP:
                var lines=el.value.replace(/\r/g,'').split('\n'),
                i=lines.length,^M
                lines_removed,^M
                char_removed;

Lines can be accessed as an associative array (e.g. if I print lines[0], I get everything up to the first carriage return entered by the user). If I just print lines, I get the entire contents of the object.

What I need to do is have a way to reference the current 'line' and insert a linebreak after n characters. I think this would then *almost* do what I want?

Any ideas much appreciated.

Thanks
 
Hi mate,

Thanks for this, but it doesn't seem to work. Is txt1 meant to be result? and also numberofChars seems to be undefined? I tried defining it myself, but it seems like it doesn't want to work.

Any ideas at all?

Thanks
 
Thanks :)

I really would like it if it were possible to contain this to a single textarea though. I have added you to MSN (from trust) to ask a question about this if it is OK?

Thanks
 
Back
Top Bottom