Expand my Community achievements bar.

Attention: Experience League Community will undergo scheduled maintenance on Tuesday, August 20th between 10-11 PM PDT. During this time, the Community and its content will not be accessible. We apologize for any inconvenience this may cause.

Line Count for multiLine text field

Avatar

Former Community Member
Everyone,



I have been racking my brain on this for days now and have been doing tons of reading, I am sure its possiable but my lack of JavaScript knowledge is really hurting.



HOW CAN I CALCULATE THE NUMBER OF LINES IN A TEXT FIELD????



Can someone PLEASE HELP!!!!!



Thanks!!!!
6 Replies

Avatar

Former Community Member
Each line except the last normally ends with the "line end" character/sequence. It might be a Cursor Return - Line Feed pair or just one or the other depending on the software. Thus once you determine what each line ends with it is simply a matter of writing a routine that searches out and counts the number of line end sequences and then add 1 for the final line.



Of course, if the software doesn't insert the line end sequence until after the data is actually removed from the field then this might not be possible. It worked in version 5 of the software using VBScript but I don't know about this version using JavaScript.

Avatar

Former Community Member
I understand that, its just developing that code is my challenge. Does anyone have a code snipet that will count the lines (count word wrap lines and returns)????

Avatar

Former Community Member
Here's code that would take the string from MyMultiLineField, count the number of carriage lines in it, and write that value into MyOutputField. Is this what you're looking for?









var myString = MyMultiLineField.rawValue

var lineCount = 1;



for (var i = 0; i < myString.length; i++)

{



if (myString[i] == '\r')



lineCount++;



}



MyOutputField.rawValue = lineCount;






Avatar

Former Community Member
Very close, but I also need it to count all lines including lines where the text wraps as well. Is that difficult to add or do you just have to add a count for \n or \f?



Thanks!!!

Avatar

Former Community Member
I think I am on to the correct answer but I can't quite get it. Here is the code i'm using:



var theArea = this

var re = new RegExp();

re.compile(".{94}","g"); //94 is the number of characters in one line (using Courier)

var replace_Area = theArea.rawValue.replace(re,"\n");

var theLines = replace_Area.split("\n");

var theLines = theArea.rawValue.split("\n");

if(theLines[theLines.length-1]=="") theLines.length--

result.rawValue = theLines.length



It works great except it does not count when a user physically presses the enter button. Any ideas?

Avatar

Former Community Member
Nevermind, i'm stupid. If i replace the \n with a \r all works. Thanks for all of your help!