• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

count amount of enters in a multiline field

Explorer ,
Dec 19, 2019 Dec 19, 2019

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;
}

 

TOPICS
Acrobat SDK and JavaScript

Views

439

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 19, 2019 Dec 19, 2019

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;

 

Votes

Translate

Translate
Community Expert ,
Dec 19, 2019 Dec 19, 2019

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;

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 22, 2019 Dec 22, 2019

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! 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2019 Dec 22, 2019

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"

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 22, 2019 Dec 22, 2019

Copy link to clipboard

Copied

LATEST

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+

  • ^ asserts position at start of a line
  • \s+ matches any whitespace character (equal to [\r\n\t\f\v ])
  • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

Second Alternative \s+$

  • \s+ matches any whitespace character (equal to [\r\n\t\f\v ])
  • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
  • $ asserts position at the end of a line

Global pattern flags

  • g modifier: global. All matches (don't return after first match)
  • m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

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:

  • \n carriage return (ASCII 13)
  • \r line-feed (newline) character (ASCII 10)
  • \t tab character (ASCII 9)
  • \f form-feed character (ASCII 12)
  • \v any vertical whitespace character

 

Thanks again and Merry Christmas. You just helped me finish my work early so i can now enjoy mine! haha

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines