Count Non-Empty Text Fields

Imported Answer

Count Non-Empty Text Fields

A form-scripting answer that demonstrates straightforward JavaScript control flow in a real document workflow.

Problem and Solution

This page carries the full answer

Problem: A form workflow needed a live count of meaningful field entries instead of a blind count of every available field.

Original question: How to count non-empty text fields? by Omar Juvera.

Full imported answer

Right-click on the text field [occupancy_count] -> Properties -> Calculate -> Custom Calculation Script -> "Edit..."

When the popup window shows up, copy and paste this javaScript code:

JS Script:

var sum = 0;
for ( i = 0; i < 9; i++ ) {
    f = "Resident" + ( i == 0? "" : i ) + "[0]";
    if (getField( f ).valueAsString) {sum += 1;}
    }
event.value = sum;

Source and Credit

Also posted on Stack Overflow

This page keeps the full solution in-house, while still pointing back to the original Stack Overflow post for proof and attribution.

Original answer: Stack Overflow answer #11378943 by Omar Juvera.

License note: this answer was originally published on Stack Overflow under CC BY-SA 3.0. Keeping a small source note here is the cleanest way to preserve attribution while the portfolio page remains the main destination.