4 Replies Latest reply on Sep 30, 2010 4:59 AM by DMFanella

    Help with Regular Expression for field validation

    DMFanella

      I'm fairly new to using regular expressions and using Acrobat. This is probably a simple question, but I've been unable to figure it out.

       

      I have a text field on a PDF that I would like to be 9 characters in length. The first 2 characters can only be alphanumeric, the last 7 characters can only be numeric.

       

      At first I was using the following, which allows all the characters to be alphanumeric:

       

      var re = /^[A-Za-z0-9 :\\_]$/;

      if (event.change.length >0) {

      if (event.willCommit == false) {

          if (!re.test(event.change)) {

              event.rc = false

          }

      }

      }

       

       

      That works fine, but it's not quite what I needed. With some assistance I changed it (see below) to fit what I was looking for. However, this didn't work; it prevents anything from being entered in the field:

       

       

      var re = /^[A-Za-z0-9]{2}\d{7}$/;

      if (event.change.length >0) {

      if (event.willCommit == false) {

          if (!re.test(event.change)) {

              event.rc = false

          }

      }

      }

       

       

      Any help would be greatly appreciated.

       

      Thanks...

        • 1. Re: Help with Regular Expression for field validation
          George_Johnson MVP & Adobe Community Professional

          Here's a function you can call form the field's custom Format script. It should be placed in a document-level JavaScript:

           

          function custom_ks1() {

           

              // Define non-commited regular expression
              var re = /^[A-Za-z0-9]{0,2}([0-9]{0,7})?$/;
            
              // Get all of the characters the user has entered
              var value = AFMergeChange(event);

           

              // Allow field to be cleared
              if(!value) return;

           

              if (event.willCommit) {
                
                  // Define commited regular expression
                  var re = /^[A-Za-z0-9]{2}[0-9]{7}$/;

           

                  if (!re.test(value)) {  // If final value doesn't match, alert user
                      app.alert("Your error message goes here.");
                      // event.rc = false
                  }
              } else {  // not commited

           

                  // Only allow characters that match the regular expression
                  event.rc = re.test(value);
              }
          }

           

          Call it like this:

           

          // Custom Keystroke script

          custom1_ks();

          • 2. Re: Help with Regular Expression for field validation
            DMFanella Level 1

            Thanks for the quick response. I have another question though...

             

            I'm using Acrobat 9 Standard and I'm unsure of how to add that as a document-level script. I did some searching and checked the hlep files, but wasn't able to figure out how.

            • 3. Re: Help with Regular Expression for field validation
              George_Johnson MVP & Adobe Community Professional

              As you've found, Standard does not provide a user enterface for creating document-level JavaScripts. Quite silly. What you can do instead is place the function in a Page Open JavaScript in the initial page. That way, the code will run when the document is opened and the function will become available for use thereafter.

               

              To add a Page Open JavaScript, open the Pages pane (View > Navigation Panels > Page), right-click on the inial page, select Properties, Actions tab, and add a new "Run a JavaScript" action. Simple paste the code that defines the function.

              • 4. Re: Help with Regular Expression for field validation
                DMFanella Level 1

                That works perfectly! Thank you for your help, I really appreciate it!