Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Jun 14, 2017 Jun 14, 2017

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.

TOPICS
Acrobat SDK and JavaScript , Windows
752
Translate
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

Community Expert , Jun 14, 2017 Jun 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

 

...
Translate
Community Expert ,
Jun 14, 2017 Jun 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;

  }

}

Translate
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 ,
Jun 14, 2017 Jun 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!

Translate
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
Community Expert ,
Jun 14, 2017 Jun 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))) {

Translate
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 ,
Jun 15, 2017 Jun 15, 2017

So where should I put this JavaScript? Does it go within the current JavaScript or at the end? This is probably a dumb question, but I'm new at this and just trying to grasp the concept.

Translate
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
Community Expert ,
Jun 15, 2017 Jun 15, 2017

You need to replace part of your existing script with these lines. Look for the "if" statement and replace that line with the new lines.

Translate
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 ,
Jun 15, 2017 Jun 15, 2017

Got it! It worked perfectly. One last question since you've been such a great help already. Is there a way to have the selections as and/or? Could I have "Field C" show when either a married or divorced male is select at the specific ages?

For example:

     "Field C" shows when a married male at the age of 45 is selected.

     "Field C" shows when a divorced male at the age of 35 is selected.

     "Field C" hides when a divorced male at the age of 48 is selected.

     "Field C" hides when a divorced female at the age of 30 is selected.

and so on...

Thank you in advance for your response. Your assistance is saving me right now!

Translate
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
Community Expert ,
Jun 15, 2017 Jun 15, 2017

Again, this is just basic JavaScript. You may want to invest some time into learning how logical operations are working in JavaScript (e.g. here Expressions and operators - JavaScript | MDN​). If you need to get more familiar with JavaScript in general, take a look here for some guidance about how to approach learning JavaScript for Acrobat: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC

Do you have another checkbox for the divorced condition? If so, then just add that to the expression:

var isDivorced = this.getField("Checkbox 3").value != "Off"

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

Translate
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 ,
Jun 15, 2017 Jun 15, 2017

It works! You are a wizard and I can't thank you enough!

Translate
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 ,
Nov 03, 2017 Nov 03, 2017
LATEST

So I got some feedback on my form. Is there any way to change the script from an age that ends in 0 or 5 to a 4 or 9? I tried changing it, but the current script is determines if the age is divisible by 5, which doesn't work with 4 or 9.

The form is currently set up to show a particular field based off of ages greater than 20 and every five years after that up to 50. For example: 20, 25, 30, 35, 40, 45, 50, 51, 52, etc.

How would I set it up to show the field if the age was 20, 24, 29, 34, 39, 44, 49, and greater than 50?

Thank you in advance for all of the assistance you've provided. My customers have been pleased with this form.

Translate
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