These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Javascript question - Adding today's Date when Initials present

ameet
Registered: Jan 6 2010
Posts: 5
Answered

Hi All,

I'm working with a javascript that populates today's date when the person enters in their initials in an adjacent field.

To provide some background:
My thinking is that the javascript checks to see if the initials field is not blank and if the initials field is not blank, populate the current field with today's date.

SA is the name of the field where the initials are entered

The javascript

var fld1 = this.getField("SA")
if (fld1 != null) {event.target.value = util.printd("yyyymmdd", new Date());}

The javascript experts will laugh as you know what I'm experiencing - Regardless of whether the SA field has text or not, the date populates.

If it helps...I placed the javascript in the Custom Calculation script field located in the Calculate Tab within Text Field Properties.

I'm using Adobe Acrobat 8.1.6/Windows

Thanks in advance to any and all who can provide guidance.

Ameet

eliza64
Registered: Jan 6 2010
Posts: 6
hi,
the javaScript Pocket Reference by OReilly Media might be helpful.Plz check
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You need to check whether the *value* of the field SA is not null (or not an empty string), not the field itself.
Also, I think you should use event.value, not event.target.value .

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Acrobat JavaScript and JavaScript are 'object' orientated languages and not variable oriented, so you not only need to access an object like a field but you may also need to provide a property or method of the object to manipulate or obtain a specific feature of the object.

You need to access the '.value' property of the file object either when declaring the variable or the testing the variable.

George Kaiser

ameet
Registered: Jan 6 2010
Posts: 5
I updated the javascript to reflect the recommendations and found something interesting.... For whatever reason the "!= null" test isn't working correctly and I don't know why

here is the original code modifed with the comments to remove the .target from event.value and to add in the .value for fld1

var fld1 = this.getField("SA")
if (fld1.value != null) {event.value = util.printd("yyyymmdd", new Date());

What I decided to an another text field with the following

var fld2 = this.getField("SA")
if (fld2.value != null) {event.value = util.printd("yyyymmdd", new Date());
else {event.value = util.printf("null test failed",1)}

Basically it should tell me if the != null part worked or a message indicating the contrary

needless to say it failed

I took it a step further with this experiment

var fld2 = this.getField("SA")
if (fld2.value != "PO") {event.value = util.printd("yyyymmdd", new Date());}
else {event.value = util.printf("null test failed",1)}

All tests worked. Whether I entered PO or anyother value for the initials in the field SA.

This led me to believe that there is something wrong with using null in this context. Is not for use with string variables?

Thanks for the insights and and any future guidance provided

Ameet
try67
Expert
Registered: Oct 30 2008
Posts: 2398
As I wrote earlier, you should check for the empty string "", instead of (or in addition to) null, so something like this:
if (fld2.value != null && fld2.value != "" ) ...

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

ameet
Registered: Jan 6 2010
Posts: 5
Thanks try67 for the recommendation of
If (fld2.value!="")
I tried it this morning and that seemed to work very well.

I did a ton of research on different string functions and decided to use

if (fld1.value.length != 0) {event.value = util.printd("yyyymmdd", new Date());}
No more use of !=null for me as that was a waste of time.

I hope this helps someone else out there!
Ameet
try67
Expert
Registered: Oct 30 2008
Posts: 2398
That is in effect the same as checking if it's an empty string.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com