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

Restrict the number of lines in a text area

Status
Not open for further replies.

mattquantic

Programmer
Mar 28, 2004
196
GB
Hi. Is it possible to restrict the number of lines in a text area using JS?
 
Using javascript you could count the number of characters in the textbox - but you cannot manage/count the number of characters on a particular line (since there is no concept of a line in this type of object).

If you need to manage a certain number of characters per line - and a certain number of lines... then use regular single-line text inputs - and concatenate the contents server-side. That solution will not require any javascript (and uses the html maxlength attribute).

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks. I'll go with the separate fields. Cheers M@)
 
Something like this.
[tt]
var linelimit=5;
//taref is the reference to the text area
if ((taref.value.match(/\r/) && taref.value.match(/\r/).length <= linelimit)) {
//admissible
} else {
alert("too many lines - do something");
}
[/tt]
 
Tsuji's code works well if the user uses a return character for each new "line" in the textarea. This is not what I notice most people doing in a textarea.

I think the smart move is to stick with multiple text inputs (and not rely on javascript). You can style them to they look like a text area (to a degree) - although this will never be perfect for all browsers.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
I've got a piece of code which will count the characters in a textarea.
In in text box tag put : -

Code:
onKeyPress="count_change(this,400);"

400 being the number of characters allowed in the textbox.

then this is the javasctipt: -

Code:
function count_change(textObj, MaxLen)
{
        Msg = textObj.value;
        Msglen= Msg.length;
//maximum length of our textarea
//      MaxLen=400;
        if (Msglen > MaxLen )
                {
                textObj.value=Msg.substring(0,MaxLen);
                textObj.blur();
//next line is to pop-up a warning box - not essential
                }
// next line is if we want to return the users focus to the textarea.
textObj.focus();
}

Hope this helps

Stewart
 
I don't know and do not want to explore all facets of thing. I just drafted it out and I have an interest to make it better on its own. Upon reflecting, there is a hole and I want to plug it. Here is the amendment.
[tt]
var linelimit=5;
//taref is the reference to the text area
if ((taref.value.match(/\r/) {
if (taref.value.match(/\r/).length <= linelimit)) {
//admissible
} else {
alert("too many lines - do something");
}
}
[/tt]
If it doesn't server a useful purpose, don't bother. On the client-side, nothing is 100% certain.
 
If you want to get fancy (by relying on javascript) you could use an onkeyup event to call a function and in that function count the number of characters.
Begin a line Count of characters until you 1. reach the maximum number of characters for that line or, 2. find a carriage return. Update your total count, you will probably want to exclude the carriage return from the total count but that depends on your app.
If you reach the max characters for a line without hitting a carriage return then insert one at that position and start counting for the new line.

I have done something almost exactly like this to format data coming from a large database text field and display in a textarea.


Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top