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

Count fields with specific text

Participant ,
May 16, 2024 May 16, 2024

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
1.2K
Translate
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
1 ACCEPTED SOLUTION
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;

View solution in original post

Translate
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 ,
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;
Translate
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

Thanks!

Translate
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

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");

 

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