Javascript code to undo Javascript read only code when signature cleared
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
Use a button with a script for this.
Copy link to clipboard
Copied
Thank you for your suggestion. Unfortunately using a button won't work for me. It needs to be tied to the signature block
Copy link to clipboard
Copied
++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.
Copy link to clipboard
Copied
Excellent! Yes, I think the mouse exit action script best fits what I am looking to do. Thank you!
Does this look like the code it is put together correctly?
// 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;
{
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();
{
if(event.target.valueAsString =="") this.resetForm();
}
}
}
Copy link to clipboard
Copied
Ok great! You're welcome.
For it to work you have to separate each of the events.
For instance, I would place just the first portion of your script in the "Sigend" tab ==> under "This script executes when the field is signed"; shown in the slide below your script.
// 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+":"+fieldName);... used in the debugger
getField(fieldName).readonly = true;
}
EXECUTES AT SIGNING TIME SLIDE:
And then just the single line of script as the Mouse Exit event under the "Actions" tab; illustrated in the next slide:
EXECUTES AFTER SIGNATURE FIELD IS CLEARED
See if that works for you.
Copy link to clipboard
Copied
Unfortunately the mouse exit code did not unlock, or make the fields editable again.
Copy link to clipboard
Copied
Hi,
Would you be able to share a copy of the file with no sensitive data on it?
I can take a look at it.

