Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Hours validation

Avatar

Level 2

I've created a timesheet with a dynamic table.  Users can add rows to record their time.  They can use more than one row per date.  They pick the date in the first cell from the date drop down.  Is there a way to script so that I can make sure they aren't putting more than 24 hours into one day?

1 Accepted Solution

Avatar

Correct answer by
Level 8

Try this:

var myObject = new Object();

//Where Table1.RepeatingRow is your repeating row

for (var a=0;a<Table1._RepeatingRow.count;a++){
//Where Table1.RepeatingRow.Date is the date field of your row
vDate=Table1.RepeatingRow.all.item(a).Date.rawValue;
//Store all the dates as properties of myObject and add the values for HoursWorked
if (typeof(myObject[vDate])=='undefined')
myObject[vDate]=0;
//Where Table1.RepeatingRow.HoursWorked is the number of hours worked field on your row
myObject[vDate]+=Table1.RepeatingRow.all.item(a).HoursWorked.rawValue;
}
//Cycle through all the properties(dates) and find the ones that are over 24
for (var i in myObject)
if (myObject[i]>24)
xfa.host.messageBox("Day "+i+" worked "+myObject[i])


 

Kyle

View solution in original post

6 Replies

Avatar

Level 8

I'm not sure if I understand your question only because I'm confused as to the correlation between your date drop down and determining how many hours one would work in a day. I'll make the assumption that you have a field in your row in which they enter their time for a given date.

In the change event of your Hours field:

if (!/\d|\./.test(xfa.event.change) || parseFloat(xfa.event.newText)>24)

     xfa.event.change="";

Hope I understood.

Kyle

Avatar

Level 2

The timesheet I'm creating has multiple rows that a user can add based on the time they work.  So for example, if they work from 6-7 in one department, 7-10 in another, and 10-12 in a third, they will create 3 rows and enter each set of time in one row.  I need a way to add the rows and verify the total of the hours does not exceed 24 hours in a single day.

Avatar

Correct answer by
Level 8

Try this:

var myObject = new Object();

//Where Table1.RepeatingRow is your repeating row

for (var a=0;a<Table1._RepeatingRow.count;a++){
//Where Table1.RepeatingRow.Date is the date field of your row
vDate=Table1.RepeatingRow.all.item(a).Date.rawValue;
//Store all the dates as properties of myObject and add the values for HoursWorked
if (typeof(myObject[vDate])=='undefined')
myObject[vDate]=0;
//Where Table1.RepeatingRow.HoursWorked is the number of hours worked field on your row
myObject[vDate]+=Table1.RepeatingRow.all.item(a).HoursWorked.rawValue;
}
//Cycle through all the properties(dates) and find the ones that are over 24
for (var i in myObject)
if (myObject[i]>24)
xfa.host.messageBox("Day "+i+" worked "+myObject[i])


 

Kyle

Avatar

Level 2

That works great thanks!!  Is there any way to get rid of the last time that was entered then?  I don't want to reset the whole form, but just the time that was entered to make it over 24 hours in one day.

Avatar

Level 8

If that's the case it would probably make more sense to test if the user has entered more than 24 hours as they are using the form (ie after they exit the field).

If that's the case use this script in the exit event of your hours worked field:

var myTime=0;

for (var a=0;a<_RepeatingRow.count;a++){

if (RepeatingRow.all.item(a).Date.rawValue==Date.rawValue)

  myTime+=RepeatingRow.all.item(a).HoursWorked.rawValue;

if (myTime>24){

  xfa.host.messageBox("The the number of hours worked for "+Date.rawValue+" exceeds 24.");

  this.rawValue=null;

  break;

}

}

Kyle

Avatar

Level 2

Thanks so much...that's exactly what I was looking for!!