Skip to main content
Known Participant
February 15, 2020
Answered

Javascript insert date to field for first time only

  • February 15, 2020
  • 2 replies
  • 3340 views

Hi all, 

 

I have a form that I want the date field to be automatically populated. 

I found a javascript online that inserts the date when i open the file. (https://codeshare.io/5g1lOB)

 

If i reopen the filled out form the next day, the date is updated.

Is there a way for the javascript to insert the date and then delete itself? 

This topic has been closed for replies.
Correct answer gkaiseril

PDF form fields have a "default value" property and if properly set can be used to only fill in the date field when the form is opened.  I would also set the format of the field to "None" and let the script apply the formatting for the date.

 

I would use the following document level script:

 

 f = this.getField ("DATE");
 if (f.value == f.defaultValue)
 {
f.value = util.printd ("yyyy-mm-dd", new Date ());

This approach also allows the form to be tested and then easily set for uploading or distribution by simply resetting the form.

}


 

2 replies

JR Boulay
Community Expert
Community Expert
February 15, 2020
gkaiserilCorrect answer
Inspiring
February 15, 2020

PDF form fields have a "default value" property and if properly set can be used to only fill in the date field when the form is opened.  I would also set the format of the field to "None" and let the script apply the formatting for the date.

 

I would use the following document level script:

 

 f = this.getField ("DATE");
 if (f.value == f.defaultValue)
 {
f.value = util.printd ("yyyy-mm-dd", new Date ());

This approach also allows the form to be tested and then easily set for uploading or distribution by simply resetting the form.

}


 

Bernd Alheit
Community Expert
Community Expert
February 15, 2020

Set the value of the field only when the field is empty.