11 Replies Latest reply on Jun 29, 2011 8:13 AM by RenéG

    Checking for a few empty fields

    RenéG Level 3

      Hi,

       

      I've got this script for checking for any empty field:

       

      function checkField(aField)

      {

          if (aField.value == "") // empty field

          {

          var msg = "No fields can be left empty.";

          app.alert(msg);

          return 0;

          }

      return 1;

      } // end of function

       

      But, in the Form I'm working on, only a portion of the fields are mandatory.

       

      What would be the script?

       

      Thanks.

        • 1. Re: Checking for a few empty fields
          George_Johnson MVP & Adobe Community Professional

          That will work for text fields for most possible values, but I would suggest changing that one line to:

           

              if (aField.valueAsString === "") // empty field

           

          You would call the function for each text field that you want to check, passing the field object. How are you currently calling this function?

          • 2. Re: Checking for a few empty fields
            RenéG Level 3

            George_Johnson wrote:

             

             

            You would call the function for each text field that you want to check, passing the field object. How are you currently calling this function?

            OK, I changed that line. But, I'm not sure how to "call the function for each text field that you want to check..."

             

            The function is called checkField and it's at the Document level.

            • 3. Re: Checking for a few empty fields
              MarkWalsh Level 4

              If you only want to validate the mandatory/required fields are not empty, you can put this at the beginning of your function (assuming that you have clicked the 'required' checkbox for all mandatory fields):

               

              if (!aField.required)

                  {

                  return 1;

                  }

              • 4. Re: Checking for a few empty fields
                George_Johnson MVP & Adobe Community Professional

                When exactly do you want to check for empty fields?

                • 5. Re: Checking for a few empty fields
                  RenéG Level 3

                  I'm lost.

                   

                  I'm a beginner at JavaScript.

                   

                  Maybe I should ask it like this:

                   

                  If I have 2 text fields that are required before the person prints the document: Text1, Text2

                   

                  What would be the script and where should I put it (in Acrobat 8:  Advanced > Document Processing > Document Javascripts...?)

                  • 6. Re: Checking for a few empty fields
                    George_Johnson MVP & Adobe Community Professional

                    The problem is you cannot use JavaScript to prevent a user from printing a document. You can add a "Print" button to the form that performs the check for empty required fields, and then issues a print command only if everything checks out. But the user can always bypass your button. You can also place code in the "Document WIll Print" event that checks the fields and alerts the user if any fields are blank, but you cannot then cancel the print job. Which approach would you like to take?

                    • 7. Re: Checking for a few empty fields
                      RenéG Level 3

                      The second approach. A warning that some fields are empty (naming the names of the fields), but the user can print the document even if he leaves those fields empty.

                      • 8. Re: Checking for a few empty fields
                        gkaiseril MVP & Adobe Community Professional

                        There is no way to pragmatically stop a print action once started, only the use can cancel it.

                         

                        You can perform any number of JavaScript actions to bring this to the users attention.

                         

                        You could pop an alert box or display a field with warning so the printout has a notation as to the error.

                        • 9. Re: Checking for a few empty fields
                          RenéG Level 3

                          I'm looking for the script that will alert the client that Text1 field or Text2 fields need to be filled out, before they go ahead and print.

                           

                          So far, I've got only the script if any fields are empty.

                          • 10. Re: Checking for a few empty fields
                            gkaiseril MVP & Adobe Community Professional

                            No simple cutting and pasting, you will need to indicate the fields to be checked and what the expected non-completed value if the field is.

                             

                            If you have it for one field name, just make the name parameter a variable, create an array of field names to process, and create a control loop to test each field name in the array.

                             

                            Assuming the fields are only text fields and the incomplete value is a null string:

                             

                            function checkFields(aFieldNames) {
                            // aFieldNames is the array of field names to process
                            // value of i element field name;
                            var sName;
                            // message for missing fields
                            var cMissingMsg = "You have left the following fields empty:\n";
                            // array for any incomplete fields;
                            var aMissing = new Array();
                            // loop through array of field names and check for a null value;
                            for(i = 0; i < aFieldNames.length; i++) {
                            // get the value for the i element field name;
                            sName = this.getField(aFieldNames[i]).valueAsString;
                            // test for a null value;
                            if(sName == "") {
                            // add field name to name of incomplete names;
                            aMissing[aMissing.length] = aFieldNames[i];
                            } // end null value;
                            } // end loop of field names;
                            // are there any incomplete fields? ;
                            if(aMissing.length > 0) {
                            // we have missing fields;
                            // build error message;
                            cMissingMsg += aMissing.join(", ");
                            // display alert
                            app.alert(cMissingMsg, 1, 0, "Incomplete Fields");
                            } // end missing fields processing
                            // return array of missing field names for any additional processing
                            return aMissing;
                            } // end checkFields function;

                            Your will print script could be:

                             

                            // will print
                            // hide waining field
                            var oWarning = this.getField("Warning");
                            oWarning.display = display.hidden;

                            // test for missing field values
                            var aMissingNames = checkFields(['Text1', 'Text2', 'Text3']);
                            // see is any fields are incomplete
                            if(aMissingNames.length > 0) {
                            // show and print field;
                            oWarning.display = display.visible;
                            }
                            // end will print;

                            • 11. Re: Checking for a few empty fields
                              RenéG Level 3

                              GKaiseril wrote:

                               

                              If you have it for one field name, just make the name parameter a variable, create an array of field names to process, and create a control loop to test each field name in the array.

                               


                              Thanks.

                               

                              I'm  not sure what that means.

                               

                              Anyway, I cut & pasted the script and changed the field names, but I'm getting this error:

                               

                              missing ; before statement

                              30 : at line 31

                               

                              And yes, the fields are all text (one is text and the other 2 numbers) and the error would be if the field is empty.