Search code examples
javascriptregexacrobat

acrobat form field validation regular expressions


Working in Acrobat X and trying to validate a field using a Regular Expression. My problem is that when I use the following in my JavaScript code:

var myRegExp = /dog/;
var myTextInput = event.value;

event.rc = true;

if (myTextInput != myRegExp) {
    app.alert("It's a regex why do I need to input '/dog/'! and not just dog");
} 

entered within the Variable tab of the Text Field Properties dialogue box - it does not see it as a Regular Expression.

That is to say; if I type 'dog' (ignore quote marks) into the live pdf field it gives an error, but if I input '/dog/' (ignore quote marks) into the field it works fine.

My Question: why is my var not being recognized as a Regular Expression.

I had read that the open '/' and close '/' (ignore quote marks) defined a Regular Expression but this does not appear to be case. What am I missing here?


Solution

  • The issue seems to be that you're testing for equality between a RegExp object and a string. Instead, you need to use the test method:

    var myRegExp = /dog/;
    var myTextInput = event.value;
    
    event.rc = myRegExp.test(myTextInput);
    

    See MDN for more documentation.

    The following operation is incorrect and will always return false:

    /dog/ == "dog";