0

I try to create a writeable PDF form using Acrobat Pro DC. I want to validate that an entry is finishing with a right suffix (XXYY). If it is not, I want an alert to pop up with the message, "Fullfill with the right license XXYY."

Using the helpful message How to validate PDF form?, I have written the following code :

if ( EndsWith(event.value) != "XXYY"){ 
    event.rc = false;
    app.alert({
        cMsg: "Fullfill with the right license XXYY",
        cTitle: "My Window Title",
        nIcon: 0,
        nType: 1
    });
}

But it does not work in pdf form : using debugger, I receive the message "TypeError: EndsWith is not a function".

What could I do to improve the code?

Antoine
  • 1
  • 1
  • If I am right, this is using Spidermonkey 1.8 engine. the documentation doesn't seem to illustrate that Endwith is a valid string function https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_reference#Strings – JoSSte Feb 11 '21 at 11:15
  • thanks for your comment that seems right. I understand I can neither use another function like substring. Would it be possible to implement the function "EndsWith" in Acrobat ? And how to do that ? – Antoine Feb 11 '21 at 13:03

1 Answers1

0

Add the endsWith polyfill to a document level script in the PDF and then all other scripts will be able to use it.

if (!String.prototype.endsWith) {
  String.prototype.endsWith = function(search, this_len) {
    if (this_len === undefined || this_len > this.length) {
      this_len = this.length;
    }
    return this.substring(this_len - search.length, this_len) === search;
  };
}
joelgeraci
  • 4,606
  • 1
  • 12
  • 19
  • Thanks. I've added a bunch of string related polyfills to my PDFs. It's like upgrading the JS engine without needing Adobe to do it. – joelgeraci Feb 12 '21 at 18:44