Skip to main content
robert.a.marsh
Known Participant
Sep 14, 2016 4:38 AM UTC
Answered

Javascript Calculations on Spawned Pages from Template

I have a form that has 5 pages that allows the addition of pages from templates. None of the calculations or auto tabs work. I'm very new to javascript and have searched many forums without success. Here is what my age script looks like, what do I need to add so when it spawns from the template it works properly. Thanks in advance for help.

var dob = util.scand("mm/dd/yyyy", this.getField("P3.AttachmentA.VIC2DOB").valueAsString);

if (dob != null) {

    var today = new Date();

    var age = today.getFullYear() - dob.getFullYear();

    var m = today.getMonth() - dob.getMonth();

    if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {

        age--;

    }      

    event.value = age;

}

else {

    event.value = "";

}

This topic is closed to new replies. Start a new post to keep the conversation going.
gkaiserilCorrect answer
Inspiring
Oct 1, 2016 6:28 PM UTC

The template coding is:

var dob = util.scand("mm/dd/yyyy", this.getField("VIC2DOB").valueAsString);

if (dob != null) {

    var today = new Date();

    var age = today.getFullYear() - dob.getFullYear();

    var m = today.getMonth() - dob.getMonth();

    if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {

        age--;

    }     

    event.value = age;

}

else {

    event.value = "";

}

The Javascript I originally inquired about was actually from the paged spawned from the template with added Javascript to calculate DOB. The original Template has no prefixes added. Hope this help and again all your help is appreciated.


From a working example, the script for the calculation on the template page for the age field:

var cPreFix = "";
// get prefix elements for spawned pages;
var aFieldName = event.target.name.split(".");
if(aFieldName.length > 2)
{
// first 2 elements of name array makeup the prefix for fields on a spawned page;
cPreFix = aFieldName[0] + "." + aFieldName[1] + ".";
}
// compute age in years;
var dob = util.scand("mm/dd/yyyy", this.getField(cPreFix + "VIC2DOB").valueAsString);
if (dob != null) {
    var today = new Date();
    var age = today.getFullYear() - dob.getFullYear();
    var m = today.getMonth() - dob.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {
        age--;
    }
    event.value = age;
}
else {
    event.value = "";
}

Your script does not suppress the "0" value when there is no entry for the date of birth field.

9 replies

Inspiring
Sep 15, 2016 6:01 PM UTC

If you are renaming your fields as you spawn the pages, you calculation scripts need to pickup the "P#." prefix on the spawned pages and adjust the field names in your calculations as needed.

Translate
robert.a.marsh
Known Participant
Sep 16, 2016 12:02 AM UTC

How do I enter the "P#." into the script, I've tried several different ways without success.

Translate
Inspiring
Sep 17, 2016 3:31 PM UTC

Does your templates have calculations in the form fields?

Have you checked to see if there are calculations for the fields on any of the spawned pages?

One can get the name of a field using the "event.target.name". For a spawned page, one to can use the "split(".")" to create and array of the name's strings between the ".".

Translate