Javascript: How to extract data from 2 fields into 1?

Community Beginner ,
Nov 19, 2018

Copy link to clipboard

Copied

I am trying to extract a start date and return date into one field to show travel date. These text-boxes are formatted as Date fields except for Travel Date. one is (departure) and the other is (return). I want to extract these two fields into one field so that the travel date text-box is: ("departure" - "return").

Below is what I currently have however it is not working.

var sDate = this.getField("text4").value;

var rDate = this.getField("text15").value;

var tDate = this.getField("dateTravel").value;

tDate.value = sDate.value + " - " + rDate.value;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

214

Likes

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

Javascript: How to extract data from 2 fields into 1?

Community Beginner ,
Nov 19, 2018

Copy link to clipboard

Copied

I am trying to extract a start date and return date into one field to show travel date. These text-boxes are formatted as Date fields except for Travel Date. one is (departure) and the other is (return). I want to extract these two fields into one field so that the travel date text-box is: ("departure" - "return").

Below is what I currently have however it is not working.

var sDate = this.getField("text4").value;

var rDate = this.getField("text15").value;

var tDate = this.getField("dateTravel").value;

tDate.value = sDate.value + " - " + rDate.value;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

215

Likes

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
Nov 19, 2018 0
Adobe Community Professional ,
Nov 19, 2018

Copy link to clipboard

Copied

You almost have it:

Use the following as the custom calculation script in your target field:

event.value = sDate + " - " + rDate;

You already got the value property of the field, so you don't have to do that again when you concatenate your strings.

Likes

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
Reply
Loading...
Nov 19, 2018 1
Community Beginner ,
Nov 19, 2018

Copy link to clipboard

Copied

I tried that however data is still not filling in the dateTravel text-box. Do you think It might be because I have the sDate & rDate text-boxes formatted as date fields?

Likes

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
Reply
Loading...
Nov 19, 2018 1
Most Valuable Participant ,
Nov 19, 2018

Copy link to clipboard

Copied

No, that's not it. Check the JS Console (Ctrl+J) for error messages, though.

Likes

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
Reply
Loading...
Nov 19, 2018 1
Community Beginner ,
Nov 26, 2018

Copy link to clipboard

Copied

Thank you, the error message I am recieving is TypeError: this.getField(...) is null 1:Field:Blur.

​Is it saying that the field I am trying to retrieve is empty or does not contain data? Because I have dates in there.

Likes

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
Reply
Loading...
Nov 26, 2018 0
Most Valuable Participant ,
Nov 26, 2018

Copy link to clipboard

Copied

That means the field name you used is incorrect.

Likes

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
Reply
Loading...
Nov 26, 2018 0
Community Beginner ,
Nov 26, 2018

Copy link to clipboard

Copied

I went back and changed the text-box field names for ease of use. I changed "text4" to "Depart_Date" and "text15" to "Return_Date"

Likes

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
Reply
Loading...
Nov 26, 2018 0
Community Beginner ,
Nov 26, 2018

Copy link to clipboard

Copied

correct code:

var sDate = getField("Depart_Date");

var rDate = getField("Return_Date");

var tDate = getField("Travel_Date");

tDate.value = sDate.value + " - " + rDate.value;

place this Javascript code under the "Return_Date" textbox

Likes

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
Reply
Loading...
Nov 26, 2018 0
Most Valuable Participant ,
Nov 26, 2018

Copy link to clipboard

Copied

That's not a good way of doing it. It means that it won't update the "Travel_Date" field when you update the "Depart_Date" field, for example.

Instead of doing it like that change the code to this and put it under the calculation event of the "Travel_Date" field:

var sDate = getField("Depart_Date");

var rDate = getField("Return_Date");

event.value = sDate.value + " - " + rDate.value;

Likes

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
Reply
Loading...
Nov 26, 2018 2
Community Beginner ,
Nov 26, 2018

Copy link to clipboard

Copied

Thank you!!! How would you go about creating date validation so that "Depart_Date < Return_Date" always?

Likes

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
Reply
Loading...
Nov 26, 2018 1
Adobe Community Professional ,
Nov 26, 2018

Copy link to clipboard

Copied

You need to convert the date strings to JavaScript date object and compare the values of the 2 fields.

var cDepartDate = this.getField("Depart_Date").value;

var cReturnDate = this.getField("Return_Date").value;

event.value = "";

var cDateFormat = "dd=mmmm=yyyy";

if(cDepartDate != "" && cReturnDate != ""){

     var oDepartDate = util.scand(cDateFormat, cDepartDate);

     var oReturnDate = util.scand(cDateFormat, cReturnDate);

     if(oReturnDate.getTime() > oDepartDate.getTime()){

          event.value = cDepartDate + " - " + cReturnDate;

     } else {

          app.alert("Return date must be same or after departure date!", 1, 0);

     }

}

You may need to adjust the date format to the date format of your fields. It is best to use a 4 digit year in the format.

Likes

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
Reply
Loading...
Nov 26, 2018 1