Skip to main content
June 27, 2019
Answered

Show/Hide message based on if a date falls between 2 input dates.

  • June 27, 2019
  • 1 reply
  • 1336 views

I have a form that has 2 date inputs representing the start and end date of a trip.

I have a 3rd date input that requests the company credit card expiration date.  I want to show/hide a message that says something along the lines of "card will expire during trip" if the expiration date of the credit card is before the end date.

In summary.

Input start date, input end date, input cc expiration date.  If cc expiration date is before end date, show a message, if not, message is hidden.

Thanks in advance.

JT

This topic has been closed for replies.
Correct answer try67

You can use this code as the custom calculation script of the field where you want to display this error message:

var startDateString = this.getField("StartDate").valueAsString;

var endDateString = this.getField("EndDate").valueAsString;

var expiryDateString = this.getField("ExpiryDate").valueAsString;

event.value = "";

if (startDateString!="" && endDateString!="" && expiryDateString!="") {

    var d1 = util.scand("mm/dd/yyyy", startDateString);

    var d2 = util.scand("mm/dd/yyyy", endDateString);

    var d3 = util.scand("mm/dd/yyyy", expiryDateString);

    if (d3.getTime()>=d1.getTime() && d3.getTime()<=d2.getTime()) event.value = "card will expire during trip";

}

Adjust the field names in the first 3 lines of the code to match the actual ones in your file.

1 reply

try67
Community Expert
Community Expert
June 27, 2019

What is the date format of those fields?

June 27, 2019

All 3 fields are MM/DD/YYYY

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 27, 2019

You can use this code as the custom calculation script of the field where you want to display this error message:

var startDateString = this.getField("StartDate").valueAsString;

var endDateString = this.getField("EndDate").valueAsString;

var expiryDateString = this.getField("ExpiryDate").valueAsString;

event.value = "";

if (startDateString!="" && endDateString!="" && expiryDateString!="") {

    var d1 = util.scand("mm/dd/yyyy", startDateString);

    var d2 = util.scand("mm/dd/yyyy", endDateString);

    var d3 = util.scand("mm/dd/yyyy", expiryDateString);

    if (d3.getTime()>=d1.getTime() && d3.getTime()<=d2.getTime()) event.value = "card will expire during trip";

}

Adjust the field names in the first 3 lines of the code to match the actual ones in your file.