Search code examples
javascriptacrobat

User to set password on click to lock/unlock fillable form


I am looking to create a form that allows a user to lock their respective fields, but also unlock them again, should they need to amend or update any of the information. The form is to then be sent back to the company, where it is completed, but the company should not be able to write/edit the user-input fields.

The closest solution I have come across is some script that requires the user to enter a password on clicking a button, and this password will then lock/unlock the form. The password however, should be set by the user, on first click of the button ideally. Currently, it is already determined within the script, meaning the company would have to share the password with the user, defeating the whole purpose of locking the fields.

This is the code as described above. Anything that gets me closer would be much appreciated:

(function() {
  // Prefix for group field names. Change to match what you want to use.

  // Rename the fields you want to lock to match this prefix (e.g., "PRIV.NAME")

  var f_prefix = "PRIV";

  // Your chosen password goes here

  var pw = "1234";

// Get a reference to the first field in the group

  var f = getField(f_prefix).getArray()[0];



  //Determine new readonly state, which is the opposite of the current state

  var readonly = !f.readonly;

  var readonly_desc = readonly ? "lock" : "unlock";



  //Prompt user for the password

  var resp = app.response({

    cQuestion: "To" + readonly_desc + "the fields, enter the password:",

    cTitle: "Enter password",

    bPassword: true,

    cLabel: "Password"

  });
  switch (resp) {

    case pw:

      getField(f_prefix).readonly = readonly;

      app.alert("The fields are now " + readonly_desc + "ed.", 3);

      break;

    case null: // User pressed Cancel button

      break;

    default: // Incorrect password

      app.alert("Incorrect password.", 1);

      break;

  }


})();


Solution

  • you cannot do it in the browser, you will need a server validation to the form and the password, otherwise it would be easy to send the form from the browser.

    see here an example of what would be the code like, the validatePassword suppose to make a request to the server for password validation:

    https://playcode.io/280470?tabs=console&style.css&output