4 Replies Latest reply on May 27, 2011 1:32 PM by maxwyss

    Validating Required Fields before Printing

    wcendro1

      I am relatively new to using Acrobat.  We are using Acrobat 9 Std to create PDFs that need to make the user complete Required fields before the form is printed.  Is there an easy script to be able to accomplish this?

       

      Our branches are using Acrobat Reader to complete and print the forms...not sure if that makes a difference.

       

      Will

        • 1. Re: Validating Required Fields before Printing
          gkaiseril MVP & Adobe Community Professional

          No, it is all custom coding since you need to decide what you want to do for a required field not being completed and what to do after you identify all incomplete required fields.

           

          You should also consider how you want to handle those instances when the user uses the print icon or prints through the menu bar.

          1 person found this helpful
          • 2. Re: Validating Required Fields before letting user move to next field
            wcendro1 Level 1

            I thought about this post more last night and needed to change it because I do need to allow for our branches to print blanks of the form for certain situations.

             

            Before pursuing the validation before printing, I was trying to display an error message if a required field was left blank before moving to the next field.  I have tried the following scripts:

             

            #1

            if(!event.target.valueAsString) {

                app.alert("This field must be complete. Please enter the Customer's Name", 3);

            }

            This script worked as I used it with 'On Blur', but after displaying the error message it went to the next field.  Then trying to back to the name field, threw up the error message for the next field (I have several fields in a row that are required).

             

            #2

            f=getField(event.target.name)

            if(f.value.length==0)

                 {

                 f.setFocus()

                app.alert("This field must be complete. Please enter the Customer's Name")

                 }

            I was using this with the 'On Blur' also, but I was getting into a loop in which the error message would not go away, and I have to kill the application.

             

            #3

            if(event.value.length<2)
                {
                //Sample Message

                 app.alert("This field must contain 9 digits.")
                event.rc=false;
                }

            I was trying this script with the validate function.  It will not work with any value less than 2.

             

            As I mentioned before, I am new at scripting, so any help would be greatly appreciated.

            • 3. Re: Validating Required Fields before letting user move to next field
              maxwyss Level 4

              It is recommended to carefully evaluate which fields really must have a value different from its default value, in order to keep the form working.

               

              About the printing, there is the willPrint event, which you could use to check whether all required fields have a value. However, by the time you have reached that event, you can no longer stop printing. If you want to use this event (which will occur no mater how you invoke the print command), you would have to mark the printout to be invalid. You could do this, for example, by placing a field over the whole page, or parts of it, where you have a text or an icon stating that the printout is not valid. You set this field to printing only, and if the result of your validation is OK, you set it to hidden.


              Of course, it would be best to offer the user a big "Print me" button, to which you can assign a validation script, which would let the user get back to the fields.


              If you are using both places, you would best create a validation function, which may return 0 if everything is OK, and another value if there are not properly filled fields. This function could look like this:

               

              function printvalidate()

              {

              var okcnt = 0 ;


              // and repeat the following 3 lines for every field

              if (this.getField("myField").value.toString() == this.getField("myField").defaultValue.toString()) {

              okcnt++ ;

              }


              // when done with validating, return okcnt:

              return okcnt ;

              }

               

              For validating the individual fields, you can either repeat those 3 lines of code, or make a loop through all fields or an array of fields you define.

               

              To do the validation, you simply call the function, and you evaluate its result:

               

              if (printvalidate() > 0) }

              // not OK, code for alerting user that the form is not properly filled comes here

              } else {

              // OK, code for continuing (or nothing) comes here

              }

               

              And that should get you going.

               

              Hope this can help.

               

              Max Wyss.

              • 4. Re: Validating Required Fields before letting user move to next field
                maxwyss Level 4

                Additonal note: If you want to force the user to fill something into the field before going to the next one, you have to be really, really careful, and make sure that there are no legitimate cases to leave the field blank.

                 

                Also, you should allow to leave the form at any time (be it that it is saved, be it that the user wants to start over, be it that the user wants to quit). In any of these cases, you must let the user leave any field, no matter whether its value is "legal". This is why the previous message is about validating before printing.

                 

                Hope this can help.

                 

                Max Wyss.