Creating a fillable form - require help with validation code using source fields and target fields
- July 15, 2025
- 1 reply
- 265 views
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!
