21 Replies Latest reply on Mar 11, 2008 7:21 AM by (Suzy_Q)

    Various Selections in Combo Box trigger required field

      Can anyone assist me in creating a multiple if/else statement? I have a combo box which lists all departments within my organization. I am needing assistance in creating an if/else statement that basically says if the department name is equal to say Finance, Accounting, Purchasing, etc. that a signature field is hidden because it is not required. However, if the department name were Information Services, Library, etc. the signature field would be required.

      I can get this to work if I use just one value (i.e. Finance), but when I add other values (and I've tried different methods) nothing happens.

      Thank you for any assistance you may provide.
        • 1. Re: Various Selections in Combo Box trigger required field
          evil8 Level 1
          In an If statement when you have multiple trues you can use the && symbols as an and.

          Example:
          var f1 = this.getField("myCheckBox").value;
          var f2 = this.getField("myTextField");

          If(f1 == "No" && "Off"){
          f2.required = false;
          }

          Often a Switch/Case statement is better.

          Example of a case statement:

          var J11 = this.getField("J.11").value;
          var J11A = this.getField("J.11-A");

          switch (J11) {

          case "Other":
          J11A.readonly = false;
          J11A.required = true
          J11A.fillColor = color.ltYellow;
          J11A.borderColor = color.transparent;
          break;
          case "Off":
          case "None":
          case "High_School":
          case "Associate's":
          case "Bachelor's":
          case "Master's":
          case "Doctorate":
          J11A.readonly = true;
          J11A.required = false;
          J11A.fillColor = color.ltGray;
          J11A.borderColor = color.transparent;
          break;
          }

          }

          Hope this helps.

          Gene
          • 2. Re: Various Selections in Combo Box trigger required field
            Patrick Leckey Level 3
            > In an If statement when you have multiple trues you can use the && symbols as an and.

            > Example:
            > var f1 = this.getField("myCheckBox").value;
            > var f2 = this.getField("myTextField");

            > If(f1 == "No" && "Off"){
            > f2.required = false;
            > }

            While your statement is technically true, your example is flawed. A checkbox field cannot have multiple different values at the same time, so your above ifcheck will always return as false (with an AND it would only process the ifcheck if f1 was equal to both "No" AND "Off", which is impossible). You also did not declare which variable you are testing after the &&, since "Off" is not 0 this will always be processed as true (if it doesn't throw a syntax error, depending on your debugger).

            You would need to use an OR in this case, and the correct syntax would be:

            var f1 = this.getField("myCheckBox").value;
            var f2 = this.getField("myTextField");

            if ( f1 == "No" || f1 == "Off" )
            {
            f2.required = false;
            }
            • 3. Re: Various Selections in Combo Box trigger required field
              Patrick Leckey Level 3
              Now, on to Suzy's question...

              I created an AcroForm with 2 objects - a combo box and a check box. The combo box had 3 options, "Option 1", "Option 2" and "Option 3". I wanted to set the checkbox to checked if either Option 1 or Option 3 were selected but not Option 2. Here is the code from the Calculate Tab of the Combo Box properties under Custom Calculation Script:

              var f1 = this.getField("Combo Box1").valueAsString;
              var f2 = this.getField("Check Box2");

              if ( f1 == "Option 1" || f1 == "Option 3" )
              {
              f2.checkThisBox(0, true);
              }
              else
              {
              f2.checkThisBox(0, false);
              }

              So Suzy ... you would need to replace the getField operation on f2 to get the field by whatever name you gave your signature field, and replace the two checkThisBox lines with:

              f2.required = true;
              or
              f2.required = false;

              depending on whether or not the valueAsString of your combo box matches the departments you chose.
              • 4. Re: Various Selections in Combo Box trigger required field
                Level 1
                Thank you both for your help! However, I am still having problems. I am working with a combo box and a signature field. Which field do I place this in to get it to work every time?

                I orginally placed it in the signature field as running a javascript, but it only worked the first time I opened the form. If I go back and change anything just to test the code, it quits working. Nothing happened when I placed it in the combo box field (I tried running a javascript, custom keystroke and validate.) I used the || in my code as it could have been any of several departments.

                Thanks again for your help!

                ???????????
                • 5. Re: Various Selections in Combo Box trigger required field
                  Level 1
                  Thank you so much! This appears to work.

                  Don't suppose you would know of a way around getting required fields to work in Adobe Reader? My boss wanted a lot of required fields, but most of our users will be using Adobe Reader and I read this weekend that Reader doesn't recognize "required" fields.

                  Again, my thanks for your help!
                  • 6. Re: Various Selections in Combo Box trigger required field
                    Level 1
                    One other question. I have a radio button and a state box. I am having problems with a required field. Basically, if the radio button is selected Yes, then the form requires a signature. If the state is outside of Texas, the form requires a signature. And, if the radio button is Yes and the state is Texas ... that would require a signature. (No matter what, if radio button is checked ... signature is required.

                    I can't get the following code to work ... any thoughts?

                    var s1 = this.getField("RBDeptHdYes").valueAsString;
                    var s2 = this.getField("State").valueAsString;
                    var s3 = this.getField(CMSignature);

                    if ( s1 == "Yes")
                    {
                    s3.required=true;

                    if (s2 != "Texas");
                    {
                    s3.required=true;

                    if (s1== "Yes" && s2 == "Texas")
                    {
                    s3.required=true;
                    }
                    else
                    {
                    s3.hidden=false;
                    }
                    }
                    }
                    • 7. Re: Various Selections in Combo Box trigger required field
                      MarkWalsh Level 4
                      It looks to me like you've nested your 'if' statements. Nothing will happen if s1 isn't 'Yes'.

                      Plus, you don't need the third 'if'. it is taken care of with the first one.

                      Does this do what you want?

                      if ( s1 == "Yes")
                      {
                      s3.required=true;

                      } else if (s2 != "Texas");
                      {
                      s3.required=true;

                      }
                      else
                      {
                      s3.hidden=false;
                      }
                      • 8. Re: Various Selections in Combo Box trigger required field
                        gkaiseril MVP & Adobe Community Professional
                        Check your JavaScript debugger console for errors.

                        var s1 = this.getField("RBDeptHdYes").valueAsString;
                        var s2 = this.getField("State").valueAsString;
                        var s3 = this.getField(CMSignature); // field name must be in quotes

                        if ( s1 == "Yes")
                        {
                        s3.required=true;

                        if (s2 != "Texas"); // no ";" here
                        {
                        s3.required=true;

                        if (s1== "Yes" && s2 == "Texas")
                        {
                        s3.required=true;
                        }
                        else
                        {
                        s3.hidden=false;
                        }
                        }
                        }

                        I might be easier to crate a control variable that can be set to true or false by each test and then test the control variable at the end.

                        var bRequired = false; // assume not required
                        this.getField("CMSignature").hidden = true; // hide field

                        var s1 = this.getField("RBDeptHdYes").value;
                        var s2 = this.getField("State").value;

                        if ( s1 == "Yes") bRequired = true; // required
                        if (s2 != "Texas") bRequired = true;
                        if (s1== "Yes" & s2 == "Texas") bRequired = true;

                        if (bRequired) this.getField("CMSignature").hidden = false; // signature is required

                        Your script has to be placed in the "mouse up" for the radio buttons and the "blur" action for the state combo box so that when any one of these fields is changed, the script will run. This means that whenever your requirements change, each location will have to be updated unless you use a document level function which is called by each field as needed and then only the function's code would need to be changed.
                        • 9. Re: Various Selections in Combo Box trigger required field
                          evil8 Level 1
                          PDL -

                          A checkbox field cannot have multiple different values at the same time...

                          Of course your right. Nice catch.

                          Gene
                          • 10. Re: Various Selections in Combo Box trigger required field
                            gkaiseril MVP & Adobe Community Professional
                            In AcroForms a check box, a group of check boxes with the same name, a radio button or aroup of radio buttons with the same name have a value "Off" when no iitem has been selected or the export value of the selected button (widget.).

                            LiveCycle Designer provides a selected and unselected value.
                            • 11. Re: Various Selections in Combo Box trigger required field
                              Level 1
                              Thanks Mark, I'm not sure why, but this isn't working either. You are correct in that if the radio button is checked - a signature is always required. If travel is out of state - signature is required. But if the radio button is checked and the state is Texas - signature is still required (because the radio button is still yes). This last part is the problem.

                              Thank you too, Geo. I couldn't get the control variable to do anything and the other code (thank you for the edits) still doesn't work the way I need it to. If the radio button is checked - the field shows required, but once I select the state as Texas required is removed.

                              All of the code makes sense to me, so I don't understand why it's not working. My conditions are:

                              if radio button is Yes - signature is always required [this part works]
                              if state is not Texas - signature is always required [this part works]
                              but if state is Texas and radio is Yes - signature is required because radio is Yes. [this part isn't working]

                              Any other thoughts?
                              • 12. Re: Various Selections in Combo Box trigger required field
                                Level 1
                                Use if ( <radio button is checked> || <state != Texas>).
                                One statement using or rather than and should be all you need.
                                • 13. Re: Various Selections in Combo Box trigger required field
                                  MarkWalsh Level 4
                                  "Thanks Mark, I'm not sure why, but this isn't working either. You are correct in that if the radio button is checked - a signature is always required. If travel is out of state - signature is required. But if the radio button is checked and the state is Texas - signature is still required (because the radio button is still yes). This last part is the problem."

                                  But your third condition (radio button is checked and state is Texas) is already handled by the fact that "...if the radio button is checked - a signature is always required" so unless I'm misunderstanding your situation, you don't need to check the state in this case. First check to see if the radio button is checked (signature is required), if not, then check to see if the state is Texas (signature is required).
                                  • 14. Re: Various Selections in Combo Box trigger required field
                                    Level 1
                                    Mark, I agree with you that it should work. The problem I'm having with this one appears to be that the other condition of being out-of-state isn't working. If radio isn't yes - but travel is out-of-state (not Texas) a signature is required because it's out-of-state. (If I select something other than Texas ... this isn't working.)

                                    Radio Yes and Out-of-State (not Texas) should always trigger the signature.

                                    My radio button is asking if the person is a department head. If so, their form always requires a signature.

                                    My form also asks what state the person is traveling to. All non-Texas travel requires a signature no matter who the traveler is.

                                    Should I add another statement to your example to cover the out-of-state condition.
                                    • 15. Re: Various Selections in Combo Box trigger required field
                                      MarkWalsh Level 4
                                      That problem is probably due to the fact that your IF statements are nested - if s1 is not 'Yes" then none of your other code runs the way it's written now. You need to move the checks for the state outside of the first IF statement.
                                      • 16. Re: Various Selections in Combo Box trigger required field
                                        Level 1
                                        I changed my code to the example you provided and added the variable information to the top???

                                        var s1 = this.getField("RBDeptHdYes").valueAsString;
                                        var s2 = this.getField("State").valueAsString;
                                        var s3 = this.getField("CMSignature");

                                        if ( s1 == "Yes")
                                        {
                                        s3.required=true;

                                        } else if (s2 != "Texas")
                                        {
                                        s3.required=true;

                                        }
                                        else
                                        {
                                        s3.hidden=false;
                                        }
                                        • 17. Re: Various Selections in Combo Box trigger required field
                                          MarkWalsh Level 4
                                          That should work. The logic is correct there. (note, you could also use marcia's suggestion and combine both conditions with an 'or')

                                          Testing that (with absolute values by setting the variables to 'Yes'/'No'and 'Texas'/'Ohio') seems to work for me. If it does not work for you, make sure your variables are getting set properly.
                                          • 18. Re: Various Selections in Combo Box trigger required field
                                            Level 1
                                            var s = this.getField("CMSignature");
                                            if (this.getField("RBDeptHdYes").value == "Yes" || this.getField("State").value != "Texas"){
                                            s.required=true;
                                            }else{
                                            s.hidden=false;
                                            }
                                            • 19. Re: Various Selections in Combo Box trigger required field
                                              Level 1
                                              I can get both of these codes to work the first time, but they don't seem to work if I go back and make a change. (Trying to simulate our users here. ))

                                              I first selected that I was a department head (radio Yes) traveling to Pennsylvania and the code worked. But, when I went back and changed my radio button to say I was not a department head - still traveling to Pennyslvania - the CM field does not show to be required.

                                              Any other thoughts? I've got the code as a mouse up in the radio button and on blur in the state combobox. I've tried just using it in the CM signature field but nothing happens when I do that.

                                              I'm really sorry this has been such a big pain :(, please know that I'm truly grateful for your assistance. I've been trying to get this right for days now and nothing seems to be working every time.
                                              • 20. Re: Various Selections in Combo Box trigger required field
                                                MarkWalsh Level 4
                                                Perhaps you need to set both properties on each condition (currently you are setting the 'required' property or the 'hidden' property only once.)

                                                var s = this.getField("CMSignature");
                                                if (this.getField("RBDeptHdYes").value == "Yes" || this.getField("State").value != "Texas"){
                                                s.required=true;
                                                s.hidden=true;
                                                }else{
                                                s.required=false;
                                                s.hidden=false;
                                                }
                                                • 21. Re: Various Selections in Combo Box trigger required field
                                                  Level 1
                                                  I had to reverse the hidden statements, but I believe you have solved the problem!!!!!!!!!!!!!!! Thank you so much!