Skip to main content
Participant
May 4, 2022
Answered

Enabling action button on password

  • May 4, 2022
  • 4 replies
  • 879 views

Hoping someone can help me with a specific requirement I have. I have included an action button in my pdf to make certain fields in my form read only. I have written the javascript code for it and it works perfectly. I need to include a second action button that would reverse the 'read-only' fields back to being eidtable but this second button should be executed only on a successful password entry. Can someone please help with the javascript code that is required for this second button?

 

Thank you!

Correct answer JR Boulay

Try this script (don't forget to set the password and the name of the field to be unlocked):

 

var cResponse = app.response({cQuestion: "Enter password", cTitle: "PASSWORD", });
{
// Cancel
if (cResponse == null) {app.alert({cMsg: "You must enter your password to unlock this field.", cTitle: "PASSWORD", nIcon: 3});}
// password OK
else if (cResponse == "123456") {this.getField("lockedField").readonly = false;}
// password KO
else {app.alert({cMsg: "Invalid password.", cTitle: "PASSWORD", nIcon: 0});}
}

4 replies

JR Boulay
Community Expert
Community Expert
March 11, 2025

If you don't want the password to be human-readable, you can use Unicode to encode it, for example for “123456” :

else if (cResponse == "\u0031\u0032\u0033\u0034\u0035\u0036") {

You can use this free tool to Uni-encode the string:

https://www.abracadabrapdf.net/utilitaires/utilitaires-pdf/texte-vers-unicode/

 

But bear in mind that this is not an absolute protection, it's just an additional difficulty for anyone who wants to know the password by analyzing the code.

Acrobate du PDF, InDesigner et Photoshopographe
Participant
February 6, 2025

What if I want to unlock multiple fields with the password? What’s the syntax for that? Something like this, perhaps?

 

var cResponse = app.response({cQuestion: "Enter the password", cTitle: "PASSWORD", });
{
// Cancel
if (cResponse == null) {app.alert({cMsg: "You must enter your password to unlock these fields.", cTitle: "PASSWORD", nIcon: 3});}
// if password is correct
else if (cResponse == "123456") {
	this.getField("LockedField1").readonly = false;
	this.getField("LockedField2").readonly = false;
	this.getField("LockedField3").readonly = false;
}
// if password is incorrect
else {app.alert({cMsg: "Invalid password.", cTitle: "PASSWORD", nIcon: 0});}
}
Souvik Sadhu
Community Manager
Community Manager
March 11, 2025

Hi @joseph_2523,

 

Hope you are doing well. Thanks for writing in!

 

If you are still looking for a solution, here is my take on it:

 

var cResponse = app.response({
    cQuestion: "Enter the password",
    cTitle: "PASSWORD"
});

// Cancel
if (cResponse == null) {
    app.alert({
        cMsg: "You must enter your password to unlock these fields.",
        cTitle: "PASSWORD",
        nIcon: 3
    });
}
// If password is correct
else if (cResponse == "123456") {
    this.getField("LockedField1").readonly = false;
    this.getField("LockedField2").readonly = false;
    this.getField("LockedField3").readonly = false;
    app.alert({
        cMsg: "Fields unlocked successfully.",
        cTitle: "SUCCESS",
        nIcon: 1
    });
}
// If password is incorrect
else {
    app.alert({
        cMsg: "Invalid password.",
        cTitle: "PASSWORD",
        nIcon: 0
    });
}

 

Please note that I am no expert when it comes to java scripting. This is just a try which might or might not work.

 

Regards,
Souvik.

JR Boulay
Community Expert
Community Expert
May 4, 2022

Keep in mind that this is a very low security level since the password is stored in the script.

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertCorrect answer
Community Expert
May 4, 2022

Try this script (don't forget to set the password and the name of the field to be unlocked):

 

var cResponse = app.response({cQuestion: "Enter password", cTitle: "PASSWORD", });
{
// Cancel
if (cResponse == null) {app.alert({cMsg: "You must enter your password to unlock this field.", cTitle: "PASSWORD", nIcon: 3});}
// password OK
else if (cResponse == "123456") {this.getField("lockedField").readonly = false;}
// password KO
else {app.alert({cMsg: "Invalid password.", cTitle: "PASSWORD", nIcon: 0});}
}

Acrobate du PDF, InDesigner et Photoshopographe
Participant
May 4, 2022

@JR Boulay Thank you! It worked perfectly! 🙂