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

Get rowcount of multiline textbox on keypress

Status
Not open for further replies.

redaccess

MIS
Aug 2, 2001
110
0
0
US
Is there a way to retrieve the current row number that a user is entering information on using javascript? My goal is to restrict user input once they've reached x number of rows in the textbox. Perhaps there's a better way to do this.....?

Anyway,
I'm using an asp.net webform with the following multiline textbox.

<asp:textbox id=&quot;txtComments&quot; onkeypress=&quot;rowcount()&quot; runat=&quot;server&quot; TextMode=&quot;MultiLine&quot; Rows=&quot;9&quot;></asp:textbox>

Thank you!
 
You can get the number of hard line breaks using the following:

document.formName.textareaName.value.split(&quot;\n&quot;).length

Keep in mind that the user can enter data by pasting it into the text box as well. You may want to consider using the onbeforepaste event to handle that scenario.

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life=life-1};
 
Thanks for the response. That does work well to get the number of line breaks however, I'd have to depend on the user using the line breaks instead of the auto-wrap. If the user just continued to type, then the rowcount would be thrown off due to the lack of &quot;\n&quot;.

Thanks again.
 

You could assume an average count of characters per-line, and work out a rough line count. Or you might be better off restricting to x characters.

Dan
 
In a textarea you can include wrap=&quot;hard&quot; so the browser will send line breaks where the text wraps. Then you can validate it on the server-side. As for a client-side solution, Dan probably has the best solution for that.

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life=life-1};
 
As I said in my original post, I'm using ASP.Net which uses a textbox with its multiple row setting to true. Therefore the wrap=&quot;hard&quot; doesn't work. However, your idea would work if there is a way to force a &quot;\n&quot; where the text wraps on an ASP.Net textbox.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top