473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to Count the number of lines?

Anz
Is there any javascript function for counting the number of lines
inside a text area ?
Jul 1 '08 #1
7 17538
On Jul 1, 3:39*am, Anz <anzbmuham...@gmail.comwrote:
Is there any javascript function for counting the number of lines
inside a text area ?
try:
a.value.split("\n").length
Jul 1 '08 #2
RoLo wrote:
On Jul 1, 3:39*am, Anz <anzbmuham...@gmail.comwrote:
>Is there any javascript function for counting the number of lines
inside a text area ?

try:
a.value.split("\n").length
I'm afraid it's not that simple. The number of lines in a textarea
does not necessarily represent the number of newline characters. You
are right that your solution should work for a <textarea wrap="off">
in MSIE and FireFox, although that will not qualify as valid X/HTML.

The original poster should first be clear whether or not the text in
his <textareashould wrap.

1) If yes:
Read out the number of columns with [obj].cols as to split the content
accordingly, but only if the wrapped string must be considered as
multiple lines. In the other case, use a.value.split("\n").length.

2) If no:
Use a.value.split("\n").length.

In both in cases, the textarea must be set with the right wrap
attributes/CSS as to achieve a maximum browser compatibility.

Hope this helps,

--
Bart
Jul 1 '08 #3
Anz wrote:
Is there any javascript function for counting the number of lines
inside a text area ?
As for actual lines, independent of presentation:

textarea.value.split(/\r?\n|\r/).length
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jul 1 '08 #4
Thomas 'PointedEars' Lahn wrote on 01 jul 2008 in comp.lang.javascript:
Anz wrote:
>Is there any javascript function for counting the number of lines
inside a text area ?

As for actual lines, independent of presentation:

textarea.value.split(/\r?\n|\r/).length
textarea.value.split(/\r?\n|\r/).length + 1

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 1 '08 #5
In comp.lang.javascript message <cd27072c-761f-4e82-a475-fd08860157c0@v1
g2000pra.googlegroups.com>, Tue, 1 Jul 2008 00:39:12, Anz
<an**********@gmail.composted:
>Is there any javascript function for counting the number of lines
inside a text area ?
In addition to what others have written, ISTM that there is a question
of whether blank lines within, before, or after non-blank lines should
be counted.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Jul 1 '08 #6
Dr J R Stockton wrote on 01 jul 2008 in comp.lang.javascript:
In comp.lang.javascript message
<cd27072c-761f-4e82-a475-fd08860157c0@v1 g2000pra.googlegroups.com>,
Tue, 1 Jul 2008 00:39:12, Anz <an**********@gmail.composted:
>>Is there any javascript function for counting the number of lines
inside a text area ?

In addition to what others have written, ISTM that there is a question
of whether blank lines within, before, or after non-blank lines should
be counted.

.... where a blank line not always is an empty line.
===================================
<textarea id=t>

qqq

sss

www

</textarea>

<script type='text/javascript'>

var r;
var t = document.getElementById('t');

r = t.value.split(/\n/).length;
alert(r); // 9
r = t.value.split(/\n*/).length;
alert(r); // 18
r = t.value.split(/[\r\n]+/).length;
alert(r); // 3
r = t.value.split(/\n[\r\n]*/).length;
alert(r); //4

</script>
==================================

18 ???

What is happening here [IE7]?

;-)
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 2 '08 #7
"Evertjan." wrote:
<textarea id=t>

qqq

sss

www

</textarea>

<script type='text/javascript'>

var r;
var t = document.getElementById('t');

r = t.value.split(/\n/).length;
alert(r); // 9
r = t.value.split(/\n*/).length;
alert(r); // 18
r = t.value.split(/[\r\n]+/).length;
alert(r); // 3
r = t.value.split(/\n[\r\n]*/).length;
alert(r); //4

</script>
==================================

18 *???

What is happening here [IE7]?
In your example,

t.value.split(/\n*/).length

is the same as

t.value.length

minus the number of newlines in 't', because a split on '\n*' can be
semantically read as "splitting on a newline that is repeated zero or
more times".

--
Bart
Jul 2 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: deko | last post by:
Below are the contents file that has the IP address and time of visit of visitors to a website. 68.122.69.241|1089822686 68.122.69.241|1089823630 68.122.69.241|1089823638
22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
1
by: laredotornado | last post by:
Hello, I am trying to decipher an XSL sheet that is not my own, and yes, I'm a newbie. <xsl:template match="sentence"> <xsl:variable name="lines" select="count(line) + count(preceding::line)"/>...
3
by: Matthias Haffke | last post by:
Ok, this is a tricky question for the pro's: My access sheet: line, id a, id b, val% ---------------- 1, a, ac, 0.04 2, a, ac, 0.28 3, a, ac, 0.015 4, a, ac, 0.205
3
by: Bruce | last post by:
Is there any mechanism in vs 2005 to have the IDE report the number of lines of source code in a C# project either per file or preferably across an entire project? Thanks, Bruce
23
by: Paul Mars | last post by:
I need to limit multiline textbox to 3 lines of text and if there are less then 3 lines when leaving, add empty line holders. How can I do this?? Thanks, paul
4
by: tommcd24 | last post by:
Does anyone know of a utility or VS add-in to count code lines in a project or solution? I just had to provide 50 pages of code for copyright and had initially thought that would be nearly the...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
13
by: humaid | last post by:
hi,guys i have done a program to count the number of bigrams. i have taken a input file by using @ARGV,then icounted the number of lines in the file,using the split function i splited the sentence...
3
by: waynejr25 | last post by:
can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of characters, words, and lines but i need the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.