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

Creating a fillable form - require help with validation code using source fields and target fields

New Here ,
Jul 15, 2025 Jul 15, 2025

I am trying to create a form that will count the number of beverages each camp student consumes for breakfast, lunch, and dinner based on the following fields:

 

StartDate - the day the student arrives and registers at camp;

 

ArrivalTime - the time they arrived at camp (before 8:30am, between 8:30am-1:30pm, or after 1:30pm);

 

EndDate – the day the camper disenrolls and departs from camp;

 

ReturnTime - the time the student departed camp (before 12:30pm, between 12:30-6:30pm, or after 6:30pm).

 

Camp_Days - the number of days student is registered in camp (which is a field that calcs the # of days using the StartDate and EndDate);

 

The target fields are: Breakfast, Lunch, Dinner

 

Example:  Mary registers at Camp Santa Cruz at 9am on July 1 and departs camp on July 5 after 6:30pm. 

 

Validation code for the Camp_Days field:

//Script to calculate difference between 2 dates and display the difference in days

 

 var dd = this.getField("StartDate").value;

 var rd = this.getField("EndDate").value;

 

 var d1 = util.scand("mm/dd/yyyy", dd);

 var d2 = util.scand("mm/dd/yyyy", rd);

 

 var diff  = (d2.valueOf() - d1.valueOf()) / 1000

 

 event.value = Math.round((diff / 60 / 60) / 24) + 1;

 

Mary is entitled to have 12 beverages in total. She was registered in camp for a total of 5 days. However, she arrived after 8:30am, missing the breakfast beverage on her first day.  Mary departs camp between 12:30-6:30pm, missing the lunch and dinner beverage on her last day.

 

Breakfast

4

Lunch

4

Dinner

4

 

Could you please help with the validation code for each of the fields, Breakfast, Lunch, and Dinner?

I already have a field that calculates the total number of beverages and the total cost, but just need the individual fields for a breakdown on breakfast, lunch, and dinner.  This is where I’m stumped and have read many posts on source fields, target fields and reference fields.  I'm teaching myself JavaScript and need much help. Thank you!

TOPICS
PDF forms
108
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 ,
Jul 15, 2025 Jul 15, 2025

So first, a calculation script it needed for this task. 

Next, you've described a complex situation, so some thought needs to be put into the approach. 

It sounds like you want to determine the number of meals attended based on arival and departure date/times. To do that a table of when meals start is needed.  

To simplify, I would divide the calculation into three parts. The arival day, the departure day, and all the days inbetween.  The only days the student may miss meals are the arival and departure.  

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
New Here ,
Jul 16, 2025 Jul 16, 2025
Thank you, Thom. I will try to create a method to the madness (...think in threes).

Please understand I am creating this form on the side of my every day duties and have never taken a JavaScript class.
Everything I've learned about JS scripting is from Google, YouTube, and the Adobe Community.

For starters, in the field ("Breakfast") I will calculate the number of drinks using the StartDate and the ArrivalTIme with an if/else statement?


Thank you,

Felicia
[City of Monterey Logo]

[Facebook icon]<> [LinkedIn icon] <> [Twitter icon] <> [Youtube icon] <> [Instagram icon] <>
www.monterey.gov<>
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 ,
Jul 16, 2025 Jul 16, 2025
LATEST

I wouldn't be too quick to get into the JavaScript language details yet. All procedural programming tasks require a specific set of tasks. Define those tasks and the implementation will follow.  This is what's called flowcharting. 

The "if" statement in any language is the basic decision structure. And yes, there are always "if"s.

 

Dealing with dates and times can be a pain.  So it's best to simplify where possible. 

An easy way to do this would be to parse the start date and end date into parts, month day and year. 

If the start and end day are the same, then process only the times. 

If they are different, then process the times to determine number of meals on those days. 

Then calculate the number of days inbetween. 

 

 I'd do the start and end date time processing by converting the time to decimal number of hours, which can be then checked against the meal times. 

And process the number of days in-between by converting the start date plus one day and the end date into date objects.

Then find the difference between the two objects.  

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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