Skip to main content
robert.a.marsh
Known Participant
September 14, 2016
Answered

Javascript Calculations on Spawned Pages from Template

  • September 14, 2016
  • 9 replies
  • 1368 views

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 has been closed for replies.
Correct answer gkaiseril

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
September 15, 2016

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.

robert.a.marsh
Known Participant
September 16, 2016

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

Inspiring
September 17, 2016

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