Skip to main content
rakeshk21205956
Inspiring
September 29, 2016
Answered

I want a submit button which when clicked first lock the fields and if the button is clicked again then it ask for password to unlock all the fields

  • September 29, 2016
  • 1 reply
  • 2176 views

Sir / Madam,

I want to make a submit button which when clicked would lock all the fields so that no editing can be done........... and if user again clicks the submit button then it should ask for password to unlock all the fields for editing.........and again user can edit the required fields.

Hope you got what i am trying to say.

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

var isReadOnly = this.getField("Text4").readonly; 

 

if (isReadOnly) { 

  (function () {

// Get one of the fields in the group

var f = getField("Text4");

// Determine new readonly state, which

// is the opposite of the current state

var readonly = !f.readonly;

var readonly_desc = readonly ? "deactivate" : "activate";

// Ask user for password

var resp = app.response({

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

cTitle: "Enter password",

bPassword: true,

cLabel: "Password"

});

switch (resp) {

case "123": // Your password goes here

getField("private").readonly = readonly;

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

break;

case null : // User pressed Cancel button

break;

default : // Incorrect password

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

break;

}

})();

 

 

  for (var i=0; i<this.numFields; i++) { 

    var fieldName = this.getNthFieldName(i); 

    if (fieldName != event.target.name) { 

      this.getField(fieldName).readonly = false; 

    } 

  } 

else { 

  for (var i=0; i<this.numFields; i++) { 

    var fieldName = this.getNthFieldName(i); 

    if (fieldName != event.target.name) { 

      this.getField(fieldName).readonly = true; 

    } 

  } 


I assume you don't know what an anonymous JavaScript function is, and that's why you define one inside the "if" statement. You use such an anonymous function to create a private scope for variables, that otherwise would end up being global. If you want to use this mechanism, you need to pull out the function definition to the top level. Also, your code set the fields to writable regardless of the password entered. I moved a few things around, and this is working for me:

(function() {

    var isReadOnly = this.getField("Text4").readonly;

    if (isReadOnly) {

        // Get one of the fields in the group

        var f = getField("Text4");

        // Determine new readonly state, which

        // is the opposite of the current state

        var readonly = !f.readonly;

        var readonly_desc = readonly ? "deactivate" : "activate";

        // Ask user for password

        var resp = app.response({

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

            cTitle: "Enter password",

            bPassword: true,

            cLabel: "Password"

        });

        switch (resp) {

            case "123": // Your password goes here

                for (var i = 0; i < this.numFields; i++) {

                    var fieldName = this.getNthFieldName(i);

                    if (fieldName != event.target.name) {

                        this.getField(fieldName).readonly = false;

                    }

                }

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

                break;

            case null: // User pressed Cancel button

                break;

            default: // Incorrect password

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

                break;

        }

    } else {

        for (var i = 0; i < this.numFields; i++) {

            var fieldName = this.getNthFieldName(i);

            if (fieldName != event.target.name) {

                this.getField(fieldName).readonly = true;

            }

        }

    }

})();

1 reply

Karl Heinz  Kremer
Community Expert
Community Expert
September 29, 2016

Create a button action that first checks to see if one of the fields that can change state from editable to read-only and back. If that field is editable, then you run a loop over all fields (with the exception of your button) and sets the readonly property to true. If the field is already readonly, then put up an input window or a custom dialog that asks for the password and then runs the same loop as before, but sets the readonly property to false.

rakeshk21205956
Inspiring
September 29, 2016

I found out javascript for prompting for password.. but not able to lock the fields'

(function () {

// Get one of the fields in the group
var f = getField("private.name");

// Determine new readonly state, which
// is the opposite of the current state
var readonly = !f.readonly;

var readonly_desc = readonly ? "deactivate" : "activate";

// Ask user for password
var resp = app.response({
cQuestion: "To " + readonly_desc + " the fields, enter the password:",
cTitle: "Enter password",
bPassword: true,
cLabel: "Password"
});

switch (resp) {

case "your_password": // Your password goes here
getField("private").readonly = readonly;
app.alert("The fields are now " + readonly_desc + "d.", 3);
break;

case null : // User pressed Cancel button
break;

default : // Incorrect password
app.alert("Incorrect password.", 1);
break;
}

})();

rakeshk21205956
Inspiring
September 29, 2016

You're not saving time. You're creating problems for your self. Some people have more than one word in their first name ("John Baptiste", for example), and will be annoyed if you force them to use just the first one, as it's not their full name.

Instead, just duplicate the fields across the pages, and the user will only have to fill it in once, and then value will be automatically copied to the rest of them.


then it will show the full name every where...