Copy link to clipboard
Copied
Hi,
I have created a code to find whether a new line has been created in a multiline text field which is working well. Although what i really want to do is to count how many a new line has actually been input. For the life of me i cant quite figure it out.
I am pretty good with javascript, but the calculation logic for counting the quantity of a specific thing in a field is a bit above my head. Any help would be appreciated.
The example below is script that i have started building, but i cant get it to work. There are no errors in the JS console but the field i put this calculation in only says "undefined"
var ENT = this.getField("multiline test").value.toString().search("\r")!=-1;
for (var i=0; i < ENT; i++){
event.target.value = ENT.value;
}
Here is a very simple method using regular expressions.
var aRslt = this.getField("multiline test").valueAsString.match(/\r/gm);
var nCnt = aRslt?aRslt.length:0;
Copy link to clipboard
Copied
Here is a very simple method using regular expressions.
var aRslt = this.getField("multiline test").valueAsString.match(/\r/gm);
var nCnt = aRslt?aRslt.length:0;
Copy link to clipboard
Copied
Wow Thom, That is amazing. I dont think i would have ever gotten there myself so thank you.
Will have to do a bit more research into the .match line of things. Do you have any good reference material as to the meaning of the "gm" in the match parameters. that way i know how i can do more in the future.
This is what i built with your help. I have created a code for adjusting the font size in a multiline field based on the amount of times enter has been hit. Is extremely useful for me for a customer form where most addresses are 2 lines but on occation 3 or more.
var aRslt = event.target.valueAsString.match(/\r/gm);
var nCnt = aRslt?aRslt.length:0;
if ( nCnt == 0 // when field is empty set text size to auto
|| // OR
nCnt >= 2) // when field has less than X number enters (eg 10)
{
event.target.textSize = 0; // text size set to auto
}else{
event.target.textSize = 18; // text size wanted.
}
Hopefully something like this could help someone else in the future. I couldnt find any code like in throughout the forums. Only script based on total character length rather than enters used.
Thanks again!
Copy link to clipboard
Copied
Here's an article that has a couple of good links at the bottom.
https://acrobatusers.com/tutorials/text-matching-regular-expressions
Also just google "Regular Expression"
Copy link to clipboard
Copied
Thanks Thom.
I read your article. Very informative.
Also found a link with a more detailed explanation of each delimitation it was in regards to trimming but now, Specifically i now understand the gm which is great.
/ Regex separator
First Alternative ^\s+
Second Alternative \s+$
Global pattern flags
You can read more details on regex101.com.
Function explanation
The function call return x.replace(/^\s+|\s+$/gm,''); searches for any spaces from the beginning of the string and from the end of string. If found then it is replaced by empty string ''. Simply said it does the trim whitespace characters:
Thanks again and Merry Christmas. You just helped me finish my work early so i can now enjoy mine! haha