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

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

Community Beginner ,
Nov 19, 2018 Nov 19, 2018

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
881
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 ,
Nov 19, 2018 Nov 19, 2018

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.

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 Beginner ,
Nov 19, 2018 Nov 19, 2018

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?

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 ,
Nov 19, 2018 Nov 19, 2018

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

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 Beginner ,
Nov 26, 2018 Nov 26, 2018

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.

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 ,
Nov 26, 2018 Nov 26, 2018

That means the field name you used is incorrect.

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 Beginner ,
Nov 26, 2018 Nov 26, 2018

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"

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 Beginner ,
Nov 26, 2018 Nov 26, 2018

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

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 ,
Nov 26, 2018 Nov 26, 2018

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;

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 Beginner ,
Nov 26, 2018 Nov 26, 2018

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

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
LEGEND ,
Nov 26, 2018 Nov 26, 2018
LATEST

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.

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