These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Counting Number of Fields with data entered

wdrspens
Registered: Jul 22 2008
Posts: 94
Answered

I have a form in which there are 17 fields for a date to be entered (booking a room - date of hire) and it would be useful to calculate the number of such fields that have been populated.

The Field names are: BookDate (which will always be populated) and then ExtraDates01, ExtraDates02 ... ExtraDates16.
I have set a document variable to 0 by the code:

function ds_countDates()
{
// initialise the count variable
var dateCount = 0
}

I have tried to insert in the "mouseup" and "blur" under the "actions" tab within properties for the 17 fields (BookDate and ExtraDates01 through ExtraDates16) the code:

dateCount++

And in the final field called NoDays I have added both in simplified field notation and (on another occasion) in custom calculation script the code:

NoDays = dateCount

But the above has not had the desired result.

I am using Acrobat 9 Professional only.

Can anyone help me with where I have gone wrong.

Many thanks

My Product Information:
Acrobat Pro 9.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
I would suggest a different approach, but the idea you have can work.

The first problem is the count variable you've set up in the function has a scope that is limited to that function, which means that code outside of the function does not have access to that variable. What you need to do is declare the variable in a document-level JavaScript and outside of a function definition:

// Initialize a document-global counter variable
var dateCount = 0;

The main problem involves the event(s) in which you've chosen to update this counter. You should choose an event that gets triggered when a field's value changes. For text fields, the best event to use is the Validate event. But I would advise that you also add code to validate the entry (e.g., is what the user entered a valid date?), as well as correctly adjust the counter if the user subsequently removes an entry.

Note that with the MouseUp event, it may not get triggered if the user enters data without using a mouse, and may get triggered if no data is entered. Similarly, the Blur event could get triggered when no data entry is made.

You can set up a custom calculation script for the NoDays field, which is probably the most straightforward approach. In such a script, you could examine the value of each date field, check to see if it's a valid date, and increment a counter for each valid entry. The final line of the script would be:

// Set the value of this field equal to the counter
event.value = dateCount;

where dateCount is your counter variable.

George
wdrspens
Registered: Jul 22 2008
Posts: 94
Thank you very much.
I have, however, run into a problem with Acrobat 9 Professional. How do I declare a variable outside of a function definition?

Advanced - Document Processing - Document JavaScripts gets me to the JavaScript Funcions box.

Although the JavaScript Editor (obtained through the same route) gives me a box stating "Create and Edit JavaScripts if I enter
// Initialize a document-global counter variable
var dateCount = 0
at the top level immediately under
//-------------------------------------------------------------
//-----------------Do not edit the XML tags--------------------
//-------------------------------------------------------------
It will not accept it and after I OK out the script does not appear to be present, certainly not where I inserted it.

I am therefore stuck with the mechanics of creating a document level script that is not a function.

Again many thanks for your help.

David
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
It appears you are getting into the "Edit All JavaScripts" and not the edit dialog for an individual document level JavaScript. You might want to look at [url=http://www.acrobatusers.com/tutorials/2007/js_document_scripts/]Entering Document Scripts[/url] by Thom Parker. To create a document level variable, declare outside of the "function(){}" statement.
See [url=http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/access_exisitng_scripts/]General Access to Existing Acrobat Scripts] by Thom Parker.


So one could enter a document level script with a name like 'Init" and paste the following code:

// initialize the count variable for the document
var dateCount = 0

function CountDays() {
dateCount++; // update the dateCount by field
// other code
return; // end processing in function
} // terminate function CountDays()

The function(s) names within a document level script need not be named the same as the script name and the document level script can contain more than one function or JavaScript statement.

Any JavaScript statement outside of the "function(){}" block will be executed when the PDF is opened and any variables declared will be scoped to the document but if a variable with the same name is decal red within a function the new definition will override the document level variable until the function terminates.

George Kaiser

wdrspens
Registered: Jul 22 2008
Posts: 94
Thank you very much. Your help is greatly appreciated. Now all I have to do is work out how to ascertain that an entry has been removed so that I can reduce the counter, and how to reset the counter once it has served its purpose.
Many thanks, once again.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
In LiveCycle Designer you can use the "HasValue()" function to see if a field has a value or not.

George Kaiser

wdrspens
Registered: Jul 22 2008
Posts: 94
Thanks, but Livecycle Designer is not available to me.
David
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Then one could check the field value to see if it is an empty string.

George Kaiser