5 Replies Latest reply on Jul 17, 2014 8:43 PM by LH.PGL

    Code for locking text fields using radio button?

    LH.PGL

      Hello,

       

      I'm creating a form to be used by some departments where I work. I need a code for locking editing in a group of text boxes by pressing a radio button once fields are filled up.

      I've just started using Acrobat XI PRO and I'm reading about Javascript as much as I can, still pretty lost though.

      Any help in coding or articles for beginners about the logic behind this would be appreciated.

       

      Thank you.

        • 1. Re: Code for locking text fields using radio button?
          MatLac Level 1

          The best source for begginer is here as I was one four years ago.

           

          What you want is rather simple.  Just name a group of field in a identation pattern or a tree-like pattern.  Lets say you have 5 fields, name them that way:

          GROUP1.PLACE.STREET

          GROUP1.PLACE.CITY

          GROUP1.PLACE.COUNTRY

          GROUP1.PERSON.NAME

          GROUP1.PERSON.AGE

           

          Then, if you want to have all fields of group 1 locked, use:

          this.getField("GROUP1").readonly = true


          if you want to target only the adress then:

          this.getField("GROUP1.PLACE").readonly = true

          • 2. Re: Code for locking text fields using radio button?
            MatLac Level 1

            I understand one of the condition for being able to lock the fields is they need to all be filled?  Now it gets a bit more complicated.  How many fields per group do you have? if you have few,

             

             

            //You place that block of code in a mouse up event of your radio button:

            //put them in an array:

            var fieldArray = new Array(this.getField("fieldOne"), this.getField("fieldTwo"), this.getField("fieldThree"));

             

            //Create an empty array of "empty fields"

            var emptyArray = new Array();

             

            //and then loop through that array while checking the condition for every item

            for (var i = 0; i < fieldArray.length; i++){

                 if (fieldArray[i].value == fieldArray[i].defaultValue){  //Checking if field is empty

                      emptyArray.push( fieldArray[i])  //putting empty fields in an array

                 }

            }

             

            if (emptyArray.length > 0){  //if there are fields in the array

                 this.getField("GROUP1").readonly = false;

                 app.alert("Some fields are still empty, please fill every fields.")

            }

            else{

                 this.getField("GROUP1").readonly = true;

            }

            • 3. Re: Code for locking text fields using radio button?
              LH.PGL Level 1

              Thanks! This one worked just fine, but I am having trouble targeting the fields.

               

              On some pages I will create some Radio buttons for locking specific text fields, check boxes and drop-down lists after being field, on other pages I would like to get the some kind of field locked up after a text field being filled.

               

              What should I change in the code above to target the fields I need "read only" after filled?

               

              Thank you.

              • 4. Re: Code for locking text fields using radio button?
                MatLac Level 1

                What you intend to do isn't clear.  To target a specific field, use the getField() method of the doc object.

                 

                this.getField("field1")

                 

                Radio button have values.  I guess yours will be "locked" and unlock".  so let's say the target field is named "field1", you would place this script in the calcuate event of "field1":

                 

                if (this.getField("radio1").value == "locked"){  //if locked is selected

                     event.target.readonly = true;

                }

                else{  //anything else including "unlocked" selected or even nothing selected

                     event.target.readonly = false;

                }

                 

                Now as I explained before, is "being filled" a CONDITION for being able to lock fields or not?  If it is, you need to add it in your code by comparing the value of each field to the defaultValue of the same fields.  It might be easier to help you if we had an idea of what you are trying to achieve.  if you could share the file by providing a web link to it, it might be easier.

                • 5. Re: Code for locking text fields using radio button?
                  LH.PGL Level 1

                  I’ve used the code across the form and it’s working, thanks! But I’m still facing trouble with those last pages.

                   

                  http://imgur.com/fOnQ6ec,bZ0T4Bs

                   

                  On page 1, I need a script for the "RadioButtonlockup" that when the user clicks it, it would lock up edition for all textfields and drop down lists on the page (they must be already filled in). When using the code you gave me I can only turn “read only” the textfields, it is not working for the drop down lists.

                   

                  http://imgur.com/fOnQ6ec,bZ0T4Bs#1

                   

                  And I am having the same situation on page 2, but instead of drop down lists, on this one, the problem is with the Check Boxes.

                   

                  What should I change in your code so that drop down lists can become read only on page 1? And check boxes on page 2? I really have to use one button on each pages.

                   

                  //

                  //

                  var fieldArray = new Array(this.getField("FIELDNAME"));

                   

                  //

                  var emptyArray = new Array();

                   

                  //

                  for (var i = 0; i < fieldArray.length; i++){

                       if (fieldArray[i].value == fieldArray[i].defaultValue){  //Checking if field is empty

                            emptyArray.push( fieldArray[i])  //putting empty fields in an array

                       }

                  }

                   

                  if (emptyArray.length > 0){  //if there are fields in the array

                       this.getField("FIELDNAME").readonly = false;

                   

                       app.alert(“Alert message.")

                  }

                  else{

                       this.getField("FIELDNAME").readonly = true;

                  }