Skip to main content
Participating Frequently
June 14, 2017
Answered

How to show a field after another field reaches specific values?

  • June 14, 2017
  • 1 reply
  • 832 views

I'm creating a form for work and I'm trying to reduce back and forth questions between clients. The form I am creating has the user input their birth date in (Field A). I already used the following script to automatically calculate the age and put it in (Field B):

event.value = "";

var dobValue = getField("dob").value;

if (dobValue!="") {

var dob = util.scand("mm/dd/yyyy", dobValue);

var today = new Date();

// compute age in milliseconds

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

// convert age to years ( millsec in sec * sec in min * min in hrs * hrs in day * days in year)

// truncate to whole years and adjust for binary floating point error

event.value = Math.floor( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

}

I'm trying to get (Field C) to show after (Field B) reaches every five years at the following values: 20, 25, 30, 35, 40, 45 and every year 50. It is important that the field only shows on the zero/five years, but remain hidden when the value is one through four and six through nine. The every year after 50 shouldn't be hard, but I imagine the other portions might be.

I'm not sure if this is even possible, but I figured someone might be able to give me a hand.

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

Modify your calculation script so that it also shows/hide the 3rd field (called "fieldC" in my sample code):

event.value = "";

var dobValue = getField("dob").value;

if (dobValue != "") {

  var dob = util.scand("mm/dd/yyyy", dobValue);

  var today = new Date();

  // compute age in milliseconds

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

  // convert age to years ( millsec in sec * sec in min * min in hrs * hrs in day * days in year)

  // truncate to whole years and adjust for binary floating point error

  event.value = Math.floor(age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

  // based on the age, show or hide "fieldC": Show it when the age is above 51

  // or when it's 20 or above and divisible by 5

  var fieldC = this.getField("fieldC");

  age = event.value;

  if (age > 50 || (age >= 20 && age % 5 == 0)) {

    fieldC.display = display.visible;

  }

  else {

    fieldC.display = display.hidden;

  }

}

1 reply

Karl Heinz  Kremer
Community Expert
Karl Heinz KremerCommunity ExpertCorrect answer
Community Expert
June 14, 2017

Modify your calculation script so that it also shows/hide the 3rd field (called "fieldC" in my sample code):

event.value = "";

var dobValue = getField("dob").value;

if (dobValue != "") {

  var dob = util.scand("mm/dd/yyyy", dobValue);

  var today = new Date();

  // compute age in milliseconds

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

  // convert age to years ( millsec in sec * sec in min * min in hrs * hrs in day * days in year)

  // truncate to whole years and adjust for binary floating point error

  event.value = Math.floor(age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

  // based on the age, show or hide "fieldC": Show it when the age is above 51

  // or when it's 20 or above and divisible by 5

  var fieldC = this.getField("fieldC");

  age = event.value;

  if (age > 50 || (age >= 20 && age % 5 == 0)) {

    fieldC.display = display.visible;

  }

  else {

    fieldC.display = display.hidden;

  }

}

Participating Frequently
June 14, 2017

Karl,

Thanks, that worked like a charm!

I may be asking too much, but what if I only wanted this to show when "Check Box 1" and "Check Box 2" are checked?

For instance, "Check Box 1" is to indicate if they're male or female, and "Check Box 2" indicates they're married or single. So, the "Field C" would only show when the user selects they are a married male at the age of 20, 25, 30, etc.

Thanks so much for the initial script. I might be asking too much now!

Karl Heinz  Kremer
Community Expert
Community Expert
June 14, 2017

This comes down to just standard JavaScript: Right now, the decision to show the field or not is made by using a JavaScript expression:

if (age > 50 || (age >= 20 && age % 5 == 0)) {

The JavaScript "if" construct expects an expression that returns a true or false, so all you need to do is add your new requirement to that expression, and it should work. To make things easier, we will define two variables that let's us check for "male" and "married":

var isMale = this.getField("Checkbox 1").value != "Off";

var isMarried = this.getField("Checkbox 2").value != "Off";

if (isMale && isMarried && (age > 50 || (age >= 20 && age % 5 == 0))) {