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

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

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

578

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
community guidelines
Community Expert ,
Nov 19, 2018 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.

Votes

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
community guidelines
Community Beginner ,
Nov 19, 2018 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?

Votes

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
community guidelines
Community Expert ,
Nov 19, 2018 Nov 19, 2018

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Beginner ,
Nov 26, 2018 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.

Votes

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
community guidelines
Community Expert ,
Nov 26, 2018 Nov 26, 2018

Copy link to clipboard

Copied

That means the field name you used is incorrect.

Votes

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
community guidelines
Community Beginner ,
Nov 26, 2018 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"

Votes

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

Votes

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
community guidelines
Community Expert ,
Nov 26, 2018 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;

Votes

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
community guidelines
Community Beginner ,
Nov 26, 2018 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?

Votes

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

Copy link to clipboard

Copied

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.

Votes

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
community guidelines