Have Calculated Field Show Blank if Previous Field is Blank

New Here ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

I’ve got a form that a user can enter up to 6 minors, if needed. The fields are name, date of birth, and age for each minor. I’ve come up with coding so that when the minors’ date of birth is entered into the date of birth field (Text57) the age field (Text58) calculates the minor’s age. The coding below is in a Custom Calculation Script in the age field (Text58).

 

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

if (dob!="") {

var today = new Date();

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

var nAgeMilliseconds = today.getTime() - dob.getTime();// compute age in milliseconds

var nAgeYears = ( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

 

if(nAgeYears < 1)

{ // change to months

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

     if(nAgeMonths < 0)

         nAgeMonths += 12;

     event.value = "(" + Math.floor(nAgeMonths)+ " mo)";

}

else

   event.value = "(" + Math.floor(nAgeYears) + ")";

}

 

This works great, but for the fields that do not have a date of birth entered (field is left blank/empty) the corresponding Age field shows (0) mo. If there is no date in the date of birth field, I’d like the Age field to also be blank/empty.

 

I’ve looked for coding in other posts and what I find doesn’t seem to work. I’ve played around trying to enter coding that I do find, in various spots in the coding, but I’m not having any luck.

 

Thanks for your help!

TOPICS
JavaScript, PDF forms

Views

120

Likes

translate

Translate

Translate

Report

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

correct answers 1 Correct Answer

Most Valuable Participant , Mar 23, 2021 Mar 23, 2021
The dob variable can't be an empty string, since it's a Date object. What you need to check is whether the value of the "Text57" field is empty. Like this:   var dobString = this.getField("Text57").valueAsString; if (dobString!="") { var dob = util.scand("mm/dd/yyyy", dobString); // rest of code } else event.value = "";

Likes

translate

Translate

Translate
Most Valuable Participant ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

The dob variable can't be an empty string, since it's a Date object. What you need to check is whether the value of the "Text57" field is empty.

Like this:

 

var dobString = this.getField("Text57").valueAsString;
if (dobString!="") {
	var dob = util.scand("mm/dd/yyyy", dobString);
	// rest of code
} else event.value = "";

Likes

translate

Translate

Translate

Report

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 ,
Mar 24, 2021 Mar 24, 2021

Copy link to clipboard

Copied

Thank you try67! It works beautifully.

Likes

translate

Translate

Translate

Report

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