2 Replies Latest reply: Mar 31, 2012 8:34 PM by pzkevco RSS

    AFExactMatch and Regular Expressions

    pzkevco Community Member

      Hi,

       

      I am working on something similar to this http://forums.adobe.com/message/4215772#4215772.

       

      My first go at all of this.  Have found George's and try67's helpful posts.  I am nearly there and now I am stumped.

       

      My Custom Format Script is this:

       

      // Custom Format script for text field

      if (event.value) event.value += "%";

       

      My Custom Keystroke Script is this:

       

      // Custom Keystroke script

      DigOnlyKS();

       

      My Custom calculation script is this:

       

      if (event.value == "") event.value = 0;

       

      My Document Javascript is this:

       

      function DigOnlyKS(){

      // Get all that is currently in the field

      var val = AFMergeChange(event);

      // Reject entry if anything but digits

      //event.rc = AFExactMatch(/\d*/, val);

      event.rc = AFExactMatch(/^100\.0|[1-9]{1,2}\.\d{1}|0\.\d{1}$/, val);

      }

       

      Valid entries should be:  100.0, 1.0-99.9, and 0.1-0.9.

       

      The Regular Expression appears to work on Regex testers on line.

       

      However, I can't enter anything in the field with the above. 

       

      If replace my Regex with the one from the link above - ^\d+\.?\d{0,2}$ - I can enter data in the field.

       

      However, if I remove the ? from the Regex - ^\d+\.\d{0,2}$ - to require (I believe) the decimal, I can't enter anything in the field.

       

      Clearly I am missing something.

       

      Thank you.

        • 1. Re: AFExactMatch and Regular Expressions
          George_Johnson CommunityMVP

          That regular expression isn't appropriate for a Keystroke script until the value has been committed, and even then it may be too restrictive (e.g., doesn't allow for ".9").

           

          The way I would be inclinded to implement this would be to set the Keystroke, Format, and Validate (not calculate) scripts to call the following functions, respectively.

           

          // Custom Keystroke script calls this
          function percent_ks() {
          
              AFNumber_Keystroke(1, 1, 0, 0, "", false);
          
          }
          
          // Custom Format script call this
          function percent_fmt() {
          
              AFNumber_Format(1, 1, 0, 0, "", false);
              if (event.value) event.value += "%";
          
          }
          
          // Custom Validate script calls this
          function percent_val() {
          
              if (event.value === "") event.value = 0;
          
              // Only allow numbers from 0 to 100
              if (+event.value >= 0 && +event.value <= 100) {
                  // round to nearest tenth
                  event.value = util.printf("%.1f", event.value);
              } else {
                  event.rc = false;
                  app.alert("Your error message goes here.", 3);
              }
          
          }
          

           

          It allows entry of values outside of the valid range, but the Validate script takes care of it. It also allows more than one digit to the right of the decimal, but the Validate script rounds it to the nearest tenth. Make sense?

           

          Edit: corrected typo

          • 2. Re: AFExactMatch and Regular Expressions
            pzkevco Community Member

            Yes.  It makes sense.

             

            Thank you!!!!