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

Count fields with specific text

Participant ,
May 16, 2024 May 16, 2024

Copy link to clipboard

Copied

I have a column of 15 rows of fields. The fields are named comp# (1-15). I want to count all of the fields with either CI, FO, L, or NS.  I have the code below, but I don't know how to incorporate the other 3 options.

 

var total = "";
for (var i=1; i<=15; i++) {
if (this.getField("comp"+i).valueAsString == "CI") total++;
}
event.value = total;
TOPICS
JavaScript , PDF forms

Views

358

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 , May 16, 2024 May 16, 2024

Use this:

var total = 0;
for (var i=1; i<=15; i++) {
var f = this.getField("comp"+i).valueAsString;
if (f == "CI" || f == "FO" || f == "L" || f == "NS") total++;}
event.value = total;

Votes

Translate

Translate
Community Expert ,
May 16, 2024 May 16, 2024

Copy link to clipboard

Copied

Use this:

var total = 0;
for (var i=1; i<=15; i++) {
var f = this.getField("comp"+i).valueAsString;
if (f == "CI" || f == "FO" || f == "L" || f == "NS") total++;}
event.value = total;

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
Participant ,
May 17, 2024 May 17, 2024

Copy link to clipboard

Copied

Thanks!

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
Participant ,
May 17, 2024 May 17, 2024

Copy link to clipboard

Copied

Sorry, 1 more question. How do I create a custom validation for this field to make sure the person only enters 1 of the 4 valid options.  Otherwise, the count would be incorrect.  I would like a popup of the error and for the incorrect value to be removed.  I've tried: 

var f = this.getField("comp1").value;
if (f != "CI" || f != "FO" || f != "L" || f != "NS" || f != "")
event.value = app.alert("Invalid entry. Enter a valid code from the options listed below");

 

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
Participant ,
May 17, 2024 May 17, 2024

Copy link to clipboard

Copied

LATEST

Sorry, I figured it out. 

event.rc = true;
if (event.value != "" && event.value != "CI" && event.value != "FO" && event.value != "L" && event.value != "NS")
{
    app.alert("Invalid entry. Enter a valid code from the options listed below.");
    event.rc = false;
}

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