Skip navigation
pzkevco
Currently Being Moderated

AFExactMatch and Regular Expressions

Mar 31, 2012 12:53 PM

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.

 
Replies
  • George Johnson
    11,664 posts
    Aug 11, 2002
    Currently Being Moderated
    Mar 31, 2012 5:39 PM   in reply to pzkevco

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points