Skip to main content
Participant
March 13, 2025
Question

Can't get custom validation script option to stay selected

  • March 13, 2025
  • 3 replies
  • 1040 views

I've used Adobe Acrobat Reader quite a bit in past, and have some experience coding, but am new to Adobe Acrobat Pro. I'm working on taking pages from an existing PDF that we have typically printed and filled in with pen and paper and converting them with Acrobat Pro into fillable forms. I have the fields all set up, but I have a series of fields that are for numbers that I need a custom validation script on. The reason is that sometimes these fields are not used, and then they have to have "N/A" put in them. If I just change the format to number, then it doesn't let me type "N/A" in the field.

 

I found a custom validation script that seems like it should work:

 

var value = event.value;
if (value === "N/A") {
    // Allow "N/A"
    event.rc = true;
} else {
    // Check if it's a valid number with up to 2 decimal places
    var regex = /^\d+(\.\d{1,2})?$/;
    if (regex.test(value)) {
        event.rc = true;
    } else {
        event.rc = false; // Reject input if not a valid number
    }
}

 

The problem is every time I select "Run custom validation script:" on the Validate tab for any of the fields, the radio button changes to that option, I can go in with the "Edit..." button and paste the code, but then if I close the Properties window or go to a different tab and come back, the selection has changed back to where the "Field value is not validated" option is selected again, and the code does not appear to have stayed (clicking back to run the custom script and choosing edit gives a blank code window).

 

I've tried changing the format category to None and doing this again, still have the same problem. Any ideas what I'm doing wrong and how to fix it so the script works? It's probably something simple and obvious, but I'm not finding it. Any help is greatly appreciated!!!

3 replies

try67
Community Expert
Community Expert
March 13, 2025

Works fine for me... Are you sure this is the EXACT code you're using?

PDF Automation Station
Community Expert
Community Expert
March 13, 2025

I don't know why your script is not staying.  It worked for me.  Do you want the user to enter numbers only, and the field to display "N/A" if the field is empty?  If so, you can use a custom format/keystroke combination:

 

Format:

AFNumber_Format(2,0,0,0,"",true);
if(!event.value)
{event.value="N/A";}

Keystroke:

AFNumber_Keystroke(2,0,0,0,"",true);

 

More information:

https://pdfautomationstation.substack.com/p/custom-format-for-pdf-fields

https://pdfautomationstation.substack.com/p/more-custom-formatting-for-pdf-fields

 

 

Participant
March 17, 2025

Tried this on the Format tab with the Custom format category, and the same thing happens when I try to enter scripts for the "Custom Format Script" or "Custom Keystroke Script". I'm beginning to wonder if where I am using Acrobat Pro has a setup that prevents use of these features to prevent problems with PDF files that might contain malicious code. Maybe it's being safe by blocking these. However I have seen that if I do an On Blur - Run a JavaScript on the Actions tab, the Javascript stays and works, so I'm not sure, because if JavaScript was just completely disabled, why would it work on the Actions tab?

Participant
April 14, 2025

Exactly the same goes for me. Did you sort this out somehow?

Community Manager
March 13, 2025

Hi 

Wait for more inputs from experts

 

Try this script:

var value = event.value.trim(); // Trim whitespace

if (!value || value === "N/A") {

    // Allow empty input or "N/A"

    event.rc = true;

} else {

    // Validate number format (up to 2 decimal places)

    event.rc = /^\d+(\.\d{1,2})?$/.test(value);

}

 

Let us know if that works

 


~Tariq

Participant
March 17, 2025

Unfortunately, no script is staying in this field, regardless of what it is. I tried this one to make sure, and as soon as I have pasted it in the Javascript editor and clicked OK, it goes back to the Validate tab of the Text Field Properties and "Field value is not validated" is again selected, and the text box below "Run custom validation script" with "Edit" to the right side is blank, and both the Edit button and text box where I would expect what I just pasted to show are greyed out unless I re-select "Run a custom validation script", and then the process repeats itself. It won't stay selected.

try67
Community Expert
Community Expert
March 17, 2025

Does it happen with just the one field, or any field, in any file?