7 Replies Latest reply: Dec 22, 2011 12:35 PM by George_Johnson RSS

    how to limit field entries to 1 or 3?

    Mayerchak Community Member

      Hi -

       

      I need a very simple java script that will limit a field's entries to 1 or 3. I believe this is a custom validation script.

       

      I don't know why acrobat requires us to use javascript for something so basic, but apparently they do, so I'd greatly appreciate any help on how to do this.

       

      If it's just a matter of putting in a standard shell and then using a GREP expression, can someone tell me how to set it up so I can customize it in the future?

       

      Learning javascript has been on my to do list for a while, but if I need to use it for something like this I think I'll have to push it to the top of the list.

       

      I've seen other scripts on the forums to do other sorts of field validation but can't tell how much of it I need to include for this.

       

      Thanks in advance for the help.

        • 1. Re: how to limit field entries to 1 or 3?
          George_Johnson ACP/MVPs

          Here's a topic from yesterday that discusses something very similar (a or d instead of 1 or 3): http://forums.adobe.com/thread/940007

           

          You should be able to adapt this to suit your needs, but if you get stuck, post again.

          • 2. Re: how to limit field entries to 1 or 3?
            Mayerchak Community Member

            Thanks - I read that, but was confused about the keystroke event function.

             

            if all I want is to require the field to contain either a 1 or a 3, would it be just this:

             

            // Get everything that's entered in the field
                var val = AFMergeChange(event);

             

            // Reject everything other than 1 or 3,
                event.rc = AFExactMatch(/^(1|3)$/i, val);

             

            What more do I need to make it a script?

             

            Do I need to start with the

            {

            and end with

            }

            ?

            • 3. Re: how to limit field entries to 1 or 3?
              George_Johnson ACP/MVPs

              The code has to be placed in a document-level function and the function then needs to be called from the field's Keystroke event. There is a good reason for this, but explaining it might just confuse things unnecessarily. If you don't know how to create a document-level JavaScript, I can provide directions, but the specifics depend on which version of Acrobat you're using. So let me know and I'll post more info.

              • 4. Re: how to limit field entries to 1 or 3?
                George_Johnson ACP/MVPs

                This topic discusses how to add a new document-level function and call it, but the details are specific to to Acrobat 9 (and maybe 8): http://forums.adobe.com/thread/940004

                • 5. Re: how to limit field entries to 1 or 3?
                  George_Johnson ACP/MVPs

                  I was actually thinking of this one: http://forums.adobe.com/message/4095955#4095955

                   

                  but the other includes other information that should be useful as well.

                  • 6. Re: how to limit field entries to 1 or 3?
                    Mayerchak Community Member

                    George,

                     

                    Quote from your other discussion:

                     

                    But you'll need to place it either in a document-level JavaScript outside of a function definition, or in the initial page's Page Open event.

                     

                    To create a new document-level JavaScript, select "Advanced > Document Processing > Document JavaScripts...", enter a name for the script (e.g., "init"), click the Add button, delete the function skeleton it gives you by default, and enter the line of code from above. Replace "your_date_field" with the actual name of your date field.

                     

                    I'm using Acrobax X for Macintosh, 10.1.1.

                     

                    Where is "Advanced > Document Processing . . . ?"

                     

                    I have this form field validation dialog - can't I just insert a script here?

                     

                    validation_dialog.jpg

                    • 7. Re: how to limit field entries to 1 or 3?
                      George_Johnson ACP/MVPs

                      In Acrobat 10, you have to select: Tools > JavaScript > Document JavaScripts

                       

                      A problem may be that you don't have the JavaScript panel set to show. If so, click the (very) small "Show or Hide Panels" icon/button at the top-right of the Tools panel to select JavaScript.

                       

                      You would normally be able to place the keystroke script code directly into the editor, but since the code I showed uses some built-in functions, you have to do it the way I described due to what I consider to be a bug in Acrobat.

                       

                      Also, you're showing the validate tab. It would be possible to validate the entry after the user commits it, and reject any invalid entries, but the keystroke script approach will prevent the user from being able to make invalid entries in the first place. Whether this is the best approach is for you to say. If you want to go with a validate script, the code would necessarily be different than the code for a keystroke script. Post again if this is what you want and I can revise the code to work as a validate script.