Skip to main content
Inspiring
March 13, 2018
Question

Inserting a comma and whitespace

  • March 13, 2018
  • 1 reply
  • 588 views

So I've been working at this for awhile.  The situation is that users complete a form inputting their Last name, First name MI.  The problem is they're supposed to include the comma between the Last name and First name but sometimes they don't.  I tested the code below in W3Schools.com "Tryit Editor", and it works fine.  It just doesn't in my PDF file.  Here is what I have so far:

function fComma() {

    var str = this.getField("01: Name").value;

    var res = str.replace(/,|\s+/, ',\n')

    this.getField("01: Name").value = res;

}

And I also don't know where to place the function.  I currently have the function at document level and tried calling it from the Actions tab, the Format tab in Custom Format Script, and I tried it in the Custom Validation Script tab.  None of them are working so I'm betting the script is messed up.

Please help.

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
March 13, 2018

The Validate or Custom Keystroke script will work, but you have to access the field values correctly. At the time of validation the field value is not yet set, so using getField gets the previous value.

replace "this.getField" with "event.value";

also set

event.rc = true;

The validation script validates the user entry. It's important to set the return code to true so Acrobat knows not to invalidate the value.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
SIPRNet11Author
Inspiring
March 13, 2018

Thom,

That worked, thank you.