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