Skip to main content
rwiamacy
Participant
February 2, 2018
Answered

Don't repopulate the Date , Mate.

  • February 2, 2018
  • 3 replies
  • 708 views

I have a field that populates a date when the form is created. I dont want it to repopulate every time the PDF opens after that.

My code:

var f = this.getField("ExamDate");

f.value = util.printd("mm/dd/yyyy)", new Date());

How do I do the null check?

This topic has been closed for replies.
Correct answer Thom Parker

A simple way to check for an empty field is to test for an empty string

if(f.value != "")

    f.value = util.printd("mm/dd/yyyy)", new Date());

This also works the same

if(f.value.length)

    f.value = util.printd("mm/dd/yyyy)", new Date());

This is the one I use cause it ignores entered spaces

if(/^\s*$/.test(f.value))

    f.value = util.printd("mm/dd/yyyy)", new Date());

However, now you have the issue of keeping a clean form available for opening. What happens when someone saves it back to the same name? No more blank form. You'll have to manually reset it and save it back.

3 replies

rwiamacy
rwiamacyAuthor
Participant
February 13, 2018

My code looks like this:

var f = this.getField("ExamDate");

if(f.value != "") 

     f.value = util.printd("mm/dd/yyyy)", new Date());

Its at the document level but the date is still updating. 

try67
Community Expert
Community Expert
February 13, 2018

You want to update the field if the value is not empty? That doesn't make much sense...

I think you want to update it if it is empty. If that's the case, change the second line of your code to:

if (f.valueAsString == "")

rwiamacy
rwiamacyAuthor
Participant
February 13, 2018

HA!!!!!! Thank yoooooooooouuuuuuuu!!!!!

Inspiring
February 2, 2018

Besides checking the value of the field, one can test the  default value of the field and set the value of default field. This approach will keep the open date if the form is cleared or reset by JavaScript of any menu option. To set the form up for saving with the date field cleared one only needs to clear the "Default value" on the options tab.

var oField  = this.getField("ExamDate");

if(oField.defaultValue = "")

{

      oField.devaultValue = util.printd("mm/dd/yyyy)", new Date());

     oField.value = oField.defautlValue;

     oField.readonly = true;

}

Thom Parker
Community Expert
Community Expert
February 2, 2018

Nice variation!! However, if you set the default value then a reset will never clear the date, and you can never get a new date on a clean form. The strategy for this really needs to be thought out. I think this type of document script only works when you have a clear form delivery system. If people are just copying it from a shared drive it should have a Form Prep button that clears the form and sets the current date.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
February 2, 2018

A simple way to check for an empty field is to test for an empty string

if(f.value != "")

    f.value = util.printd("mm/dd/yyyy)", new Date());

This also works the same

if(f.value.length)

    f.value = util.printd("mm/dd/yyyy)", new Date());

This is the one I use cause it ignores entered spaces

if(/^\s*$/.test(f.value))

    f.value = util.printd("mm/dd/yyyy)", new Date());

However, now you have the issue of keeping a clean form available for opening. What happens when someone saves it back to the same name? No more blank form. You'll have to manually reset it and save it back.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often