18 Replies Latest reply on Jun 3, 2018 2:41 PM by try67

    Add Condition to Submit Button Based on Required Fields

    Wallenbees Level 1

      Hey all. I have been scouring forums all morning for an answer to this but keep coming up empty. First of all, I am still VERY novice at javascript in Acrobat, but here is the scenario:

       

      I have 5 required fields in my form. When I have a submit button that just uses the mouse up - submit form event, it will reject the submission if a required field is empty. However, I am submitting using javascript so that I can add text from one of the fields to the subject of the email that this is submitted by. The issue is when it is submitted this way, it no longer seems to validate the form to ensure the required fields are filled.

       

      Some addition context:

      The required fields are largely text and are as follows:

      repname (text)

      pneumonic (text)

      customer id (text)

      client name (text)

      Verification Method1 (drop down)

       

      If all of these are filled out then the form should allow submission. In a perfect world, if they click submit and any of the fields are empty I would love a pop up message that lets them know that field is missing and required.

       

      If it is easier, I am ok with having the button be read only until those fields are filled. I feel like this should be really easy, but I can't seem to make it work. What do you all think?

        • 1. Re: Add Condition to Submit Button Based on Required Fields
          try67 MVP & Adobe Community Professional

          It's certainly possible to validate the required fields using a script. Try searching around the forums. This issue was discussed many times and I've provided code that does it to multiple people.

          • 2. Re: Add Condition to Submit Button Based on Required Fields
            Wallenbees Level 1

            Here is what I was able to create. It all works with the exception of the 4th and 6th fields:

             

            var fld1 = this.getField('repname');
            var fld2 = this.getField('Pneumonic');
            var fld3 = this.getField('Customer ID');
            var fld4 = this.getField('Verification Method1');
            var fld5 = this.getField('Client Name');
            var fld5 = this.getField('Group2');
            {
            if (fld1.value == "")
            {
            app.alert("Please enter the name of the Submitting Representative.");
            fld1.setFocus();
            }
            if (fld2.value == "")
            {
            app.alert("Please enter your Branch Pneumonic");
            fld2.setFocus();
            }
            if (fld3.value == "")
            {
            app.alert("Please enter a Customer ID.");
            fld3.setFocus();
            }
            if (fld4.value == "[Select]")
            {
            app.alert("Please include client verification method.");
            fld4.setFocus();
            }
            if (fld5.value == "")
            {
            app.alert("Please provide client name.");
            fld5.setFocus();
            }
            if (fld6.value = "No")
            {
            app.alert("Suitability must be updated in the previous 12 months to request a prefilled application.");
            fld6.setFocus();
            }

            if (fld1.value != "" && fld2.value != "" && fld3.value != "" && fld4.value != "" && fld5.value != "" && fld6.value != "Yes")
            {
            var r = app.alert("Please note by submitting this form you are confirming all information is accurate and all required fields have been completed. Do you want to submit?",2,2);
            if (r==4){
            this.mailDoc({bUI:false, cTo:email@email.com,
            cSubject: this.getField("Pneumonic").value + " Prefill Request" ,
            cMsg: "Please complete the prefill request attached to this email.",
            cSubmitAs: "PDF"});
            };
            }
            }

             

            What am I doing wrong? Additionally, if there are multiple items incorrect is it possible to include them all on one message?

            EDIT: Important information - Field 4 is a drop down and Field 6 is a radio button. I think the fact that they aren't text boxes is throwing it off.

            • 3. Re: Add Condition to Submit Button Based on Required Fields
              try67 MVP & Adobe Community Professional

              You should compare the values to the defaultValue property, instead of a hard-coded one.

              Also, you have an error in this line of code:

              if (fld6.value = "No")

              It should be:

              if (fld6.value == "No")

              • 4. Re: Add Condition to Submit Button Based on Required Fields
                Wallenbees Level 1

                I hate to say I don't quite understand that first line of feedback. At first I thought you meant "2" instead of "No" on the Yes/No radio button, but that didn't fix anything. What should the default values look like in these lines?

                 

                Also do you have any insight in combining those messages to a single output?

                 

                I fixed the error you noticed. Thank you so much for the help!

                • 5. Re: Add Condition to Submit Button Based on Required Fields
                  try67 MVP & Adobe Community Professional

                  I meant you should use something like this:

                  if (fld6.valueAsString == fld6.defaultValue)

                   

                  And I don't follow your last request about a single output... Do you mean a single error message, instead of one for each field?

                  1 person found this helpful
                  • 6. Re: Add Condition to Submit Button Based on Required Fields
                    Wallenbees Level 1

                    Ok great! That worked.

                     

                    I think I have an issue with my if statement now:

                    if (fld1.value != "" && fld2.value != "" && fld3.value != "" && fld4.value != "" && fld5.value != "" && fld6.value != "")

                     

                    Because with using the defaulvalue it still executes the if statement if there is an error with fld6. Do I need to change that so that it only goes if the value == "Yes"?

                     

                    And as far as output messages, you are exactly correct. If there are say, 2 errors, instead of getting two separate messages, the ideal state would be getting one message with both errors.

                    • 7. Re: Add Condition to Submit Button Based on Required Fields
                      try67 MVP & Adobe Community Professional

                      You should use defaultValue (notice the capital V!) there as well.

                      As for the combined message, it's possible, but then which field should get the focus?

                      1 person found this helpful
                      • 8. Re: Add Condition to Submit Button Based on Required Fields
                        Wallenbees Level 1

                        That worked beautifully!! Thank you!!

                         

                        Honestly the form is short enough that set focus isn't absolutely necessary. So even if it always defaulted back to fld1 would be fine.

                        • 9. Re: Add Condition to Submit Button Based on Required Fields
                          try67 MVP & Adobe Community Professional

                          Post your current code and I'll help you implement it.

                          • 10. Re: Add Condition to Submit Button Based on Required Fields
                            Wallenbees Level 1

                            var fld1 = this.getField('schwabrepname');
                            var fld2 = this.getField('Pneumonic');
                            var fld3 = this.getField('Customer ID');
                            var fld4 = this.getField('Verification Method1');
                            var fld5 = this.getField('Client Name');
                            var fld6 = this.getField('Suitability Group');
                            {
                            if (fld1.value == "")
                            {
                            app.alert("Please enter the name of the Submitting Representative.");
                            fld1.setFocus();
                            }
                            if (fld2.value == "")
                            {
                            app.alert("Please enter your Branch Pneumonic");
                            fld2.setFocus();
                            }
                            if (fld3.value == "")
                            {
                            app.alert("Please enter a Customer ID.");
                            fld3.setFocus();
                            }
                            if (fld4.valueAsString == fld4.defaultValue)
                            {
                            app.alert("Please include client verification method.");
                            fld4.setFocus();
                            }
                            if (fld5.value == "")
                            {
                            app.alert("Please provide client name.");
                            fld5.setFocus();
                            }
                            if (fld6.valueAsString == fld6.defaultValue)
                            {
                            app.alert("Suitability must be updated in the previous 12 months to request a prefilled application.");
                            fld6.setFocus();
                            }

                            if (fld1.value != "" && fld2.value != "" && fld3.value != "" && fld4.value != "" && fld5.value != "" && fld6.value != defaultValue)
                            {
                            var r = app.alert("Please note by submitting this form you are confirming all information is accurate and all required fields have been completed. Do you want to submit?",2,2);
                            if (r==4){
                            this.mailDoc({bUI:false, cTo:email@email.com,
                            cSubject: this.getField("Pneumonic").value + " Prefill Request" ,
                            cMsg: "Please complete the prefill request attached to this email.",
                            cSubmitAs: "PDF"});
                            };
                            }
                            }

                             

                            Here is the current code. Thank you so much for all the help!

                            • 11. Re: Add Condition to Submit Button Based on Required Fields
                              try67 MVP & Adobe Community Professional

                              Use this:

                               

                              var errMsgs = [];
                              var firstBadField = null;
                              
                              if (fld1.valueAsString == fld1.defaultValue) {
                                  errMsgs.push("Please enter the name of the Submitting Representative.");
                                  firstBadField = fld1;
                              }
                              
                              if (fld2.valueAsString == fld2.defaultValue) {
                                  errMsgs.push("Please enter your Branch Pneumonic.");
                                  firstBadField = fld2;
                              }
                              
                              // etc.
                              
                              if (errMsgs.length>0) {
                                  app.alert(errMsgs.join("\n\n"));
                                  firstBadField.setFocus();
                              } else {
                                  // insert the code to mail the form here
                              }
                              
                              1 person found this helpful
                              • 12. Re: Add Condition to Submit Button Based on Required Fields
                                Wallenbees Level 1

                                You are amazing! It works beautifully! Thank you so much!

                                • 13. Re: Add Condition to Submit Button Based on Required Fields
                                  anitaw56661563 Level 1

                                  I'm trying to do something pretty basic - or most people will think it's basic (I'm a novice).  I have a form with required fields, but I have 2 boxes that will determine if a 3rd field is required.  Example:  my form is to add or change a form.  There is a check box for "Add" and a check box for "Change".  If the form is to change an existing account then the "vendor number" is going to be a required field.  Could you possibly help me write the script for this to work?

                                   

                                  Any help is greatly appreciated.

                                  • 14. Re: Add Condition to Submit Button Based on Required Fields
                                    try67 MVP & Adobe Community Professional

                                    Are these check-boxes in the same group? What are their export values?

                                    Assuming they are, and are called "add/change" and the values are "Add" and "Change", create a hidden text field with this calculation code:

                                    this.getField("vendor number").required = (this.getField("add/change").valueAsString=="Change");

                                    • 15. Re: Add Condition to Submit Button Based on Required Fields
                                      anitaw56661563 Level 1

                                      The "add" "change" boxes are not in the same group.  They are separate boxes so someone can check either one.  Would you be able to help me write a script for that?

                                       

                                      Thanks!

                                      • 16. Re: Add Condition to Submit Button Based on Required Fields
                                        try67 MVP & Adobe Community Professional

                                        In that case the "Add" box is irrelevant. What's the name and "on" value of the "Change" box?

                                        • 17. Re: Add Condition to Submit Button Based on Required Fields
                                          anitaw56661563 Level 1

                                          I get the following error

                                          TypeError: this.getField(...) is null

                                          1:Field:Calculate

                                          TypeError: this.getField(...) is null

                                          1:Field:Calculate

                                           

                                          Not sure what I'm doing wrong.  Thanks!

                                          • 18. Re: Add Condition to Submit Button Based on Required Fields
                                            try67 MVP & Adobe Community Professional

                                            Youv'e entered an incorrect field name in the first line of your code. Remember that JS is case-sensitive, so if the field is called "Change" it won't work if you enter "change" in your code.