Skip to main content
philebwn
New Participant
February 3, 2023
Question

Javascript code to undo Javascript read only code when signature cleared

  • February 3, 2023
  • 1 reply
  • 1559 views

I have the following Javascript code to be able to auto populate date fields when a form is signed, and to make all fields, except siganture fields, when the form is signed. The problem I have is when I clear the triggering signature field, the form is still read only. Is there a string of script I can add to undo the read only script / make the form fillable/editable again when the triggering signature is cleared? :

// JavaScript code to add the date at signing time

var currentTime = new Date()

var month = currentTime.getMonth() + 1

var day = currentTime.getDate()

var year = currentTime.getFullYear()

var signingTime = month +"/"+day+"/"+year //Modify into your preferred format

var f = this.getField("Date Signed"); //Modify the field name as necessary

f.value = signingTime;

//getField("Text_Field").readonly = true;... makes Text_Field a read only field
for(var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
if (fieldName == "Print Form" ||
fieldName == "Submit Form" ||
fieldName == "Lock Form" ||
this.getField(fieldName).type == "signature")
{
//console.println(i+":no:"+fieldName);... used in the debugger
}
else {
//console.println(i+":"+fieldName);... used in the debugger
getField(fieldName).readonly = true;
}
}

 

Thank you in advance!

This topic has been closed for replies.

1 reply

Bernd Alheit
Braniac
February 3, 2023

Use a button with a script for this.

philebwn
philebwnAuthor
New Participant
February 4, 2023

Thank you for your suggestion. Unfortunately using a button won't work for me. It needs to be tied to the signature block

ls_rbls
Braniac
February 4, 2023

++Adding to the topic,

 

The signature field object won't work with as a listener event the way you have it run from the target signature field.

 

As suggested earlier by @Bernd Alheit , you need to incorporate another button with a Mouse-Up  javascript action or you may simply opt to use a hidden read-only field with a Validating script or Custom Calculation Script to make the desired action appear as if it is tied up with the signature as soon as the user interacts with it to clear it.

 

You may achieve this in different ways with Acrobat JavaScript. As a result, in my examples below I used a couple of script methods in the absence of the feature that you are asking about.

 

For instance, you may try a custom calculation script executing from the hidden text field like so:

 

 

 

 

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

if (f.value =="") this.resetForm();

 

 

 

 

You may also do the same with a custom validating script as shown in my next example. 

 

I am using a longer script based on what you've shared and I am running it as a custom validating script ( also executed in the hidden field which  produces the same result):

 

 

 

 

var checkSignature = this.getField("mySigningField");

 for(var i=0; i<this.numFields; i++) 
 {
  var fname = this.getNthFieldName(i);
  var f = this.getField(fname);

   if(f.type !="button" && f.readonly == true) 
   {
    if(checkSignature.value ="") 
    {
     this.resetForm();
    }
   }
 }
 

 

 

 

 

 

However, if you need this workflow to be exclusively tied up with the signature field (no additional buttons or hidden fields), then the single line of script shown below can do the trick as a "Mouse Exit" action:

 

 

 

 

if(event.target.valueAsString =="") this.resetForm();

 

 

 

 

This last method assumes that only one signature field object exists in that PDF.

 

If you have more than one signature fields and you wish to keep them as read-only soon after the target signature field is cleared, then you must define  through the "this.resetForm()" function just  the field objects that you want to clear (keeping the additional signature fields untouched as read-only).

 

I think this last method is more in-tune with your desired workflow. So, when the user clears the signature field, and as soon as the mouse pointer is moved outside of that signature field, it triggers the reset form action.