Skip to main content
Inspiring
August 14, 2017
Answered

show and hide fields based on a date range

  • August 14, 2017
  • 2 replies
  • 4016 views

Hello, I have been struggling with this for a while and can't find my exact situation out there.

How can I hide 2 text fields and 2 checkbox fields based on a particular date range?

I have a form that includes a date field,  after a certain date I don't want the fields to be available so I would like to hide those fields.

I tried the following, but I don't think I have the date syntax right. What am I doing wrong?

if(this.getField("Date").value >= "9/21/17"){   

this.getField("Text3").display = display.hidden;

}

else{

this.getField("Text3").display = display.visible;

}

Thank you so much in advance!

This topic has been closed for replies.
Correct answer Darlhouse11

That's the value when it's ticked. When it's not ticked the value is Off, resulting in NaN (Not a Number).

The solution is either to use some other kind of field, or to adjust the code, as I've described above.


Ok, so I replaced the field name with a number value in the script and it all works great. THANK YOU!!!

Now the last part needed for this form is to incorporate 2 late fees on the last section - if after 9/21/2017 the late fee is 25% and if after 10/21/2017 the late fee is 50%. I am not sure how to add the second late fee to the script you gave me.

This is my attempt, it's working but not calculating the 1.5 correctly, it's multiplying 1.5 by the result of the first late fee (125) resulting in a total of 187.50 and it should be 150...how can I fix this?

var sub = 100 * Number(this.getField("numEthernet").value);  // Replace with actual field names

var s = this.getField("Date").valueAsString; // Replace with actual field name

var t = this.getField("Date").valueAsString;

if (s!="") {

    var d = util.scand("mm/dd/yyyy", s);

    var cutOffDate = util.scand("mm/dd/yyyy", "9/21/2017");

    if (d.getTime()>=cutOffDate.getTime())

        sub *= 1.25;

    }

if (t!="") {

    var d = util.scand("mm/dd/yyyy", t);

    var cutOffDate1 = util.scand("mm/dd/yyyy", "10/21/2017");

    if (d.getTime()>=cutOffDate1.getTime()){

        sub *= 1.5;

    }

}

event.value = sub;

I tried again and figured it out, this is what I used and it works great!

var sub = 100 * Number(this.getField("numEthernet").value);  // Replace with actual field names

var sub1 = 100 * Number(this.getField("numEthernet").value);

var s = this.getField("Date").valueAsString; // Replace with actual field name

var t = this.getField("Date").valueAsString;

if (s!="") {

    var d = util.scand("mm/dd/yyyy", s);

    var cutOffDate = util.scand("mm/dd/yyyy", "9/21/2017");

    if (d.getTime()>=cutOffDate.getTime()) 

        sub *= 1.25;

    }

if (t!="") {

    var d = util.scand("mm/dd/yyyy", t);

    var cutOffDate1 = util.scand("mm/dd/yyyy", "10/21/2017");

    if (d.getTime()>=cutOffDate1.getTime()){ 

        sub1 *= 1.5;

    }

}

event.value = sub1;

THANK YOU SO MUCH FOR ALL YOUR HELP AND QUICK REPLIES!!!!!

2 replies

Inspiring
August 15, 2017

Thank you so much, that worked perfectly!! I have been struggling with this for a while, and I appreciate the tutorials as well.

I figured out how to add the additional fields, however 2 of the fields are checkboxes and I get an error on those fields. Do I need to do something different than I have below?

var s = event.value; 

if (s=="") {

this.getField("Text3").display = display.visible; // what should happen if the field is empty? 

this.getField("Text6").display = display.visible;

this.getField("chkBx1").display = display.visible;

this.getField("chkBx2").display = display.visible;

}

else { 

    var d = util.scand("mm/dd/yyyy", s); // I'm assuming this is the date format of your field... If not, adjust this line 

    var cutOffDate = util.scand("mm/dd/yyyy", "9/21/2017"); // It's never a good idea to use only two digits for the year... 

    if (d.getTime()>=cutOffDate.getTime()){  

        this.getField("Text3").display = display.hidden;

        this.getField("Text6").display = display.hidden;

        this.getField("chkBx1").display = display.hidden;

        this.getField("chkBx2").display = display.hidden; 

    } else { 

    this.getField("Text3").display = display.visible;

    this.getField("Text6").display = display.visible;

    this.getField("chkBx1").display = display.visible;

    this.getField("chkBx2").display = display.visible; 

    } 

Thank you again! Have a great evening!!

try67
Community Expert
Community Expert
August 15, 2017

Should work just fine with any kind of field... What error message are you getting, exactly?

Inspiring
August 16, 2017

This is the error I get in the Javascript console and it isn't hiding the checkbox fields.

try67
Community Expert
Community Expert
August 14, 2017

Working with Date objects in Acrobat is a bit tricky... Here are some good tutorials about it:

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-3

As for your request, try this code (make sure you fill in the blank value on line #2, depending on what you want to happen when the date field is empty):

var s = event.value;

if (s=="") this.getField("Text3").display = display.???; // what should happen if the field is empty?

else {

    var d = util.scand("mm/dd/yy", s); // I'm assuming this is the date format of your field... If not, adjust this line

    var cutOffDate = util.scand("mm/dd/yyyy", "9/21/2017"); // It's never a good idea to use only two digits for the year...

    if (d.getTime()>=cutOffDate.getTime()){

        this.getField("Text3").display = display.hidden;

    } else {

    this.getField("Text3").display = display.visible;

    }

}

Place this code as the custom Validation script of the Date field, by the way.