Skip to main content
Participant
March 13, 2025
Question

Can't get custom validation script option to stay selected

  • March 13, 2025
  • 6 replies
  • 1123 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!!!

6 replies

ls_rbls
Community Expert
Community Expert
April 26, 2026

Hello ​@analytical_enigma8853 ​@matthieu_7164 ​@unintended4244 ​@sophia_3546  ,

 

I've noticed this happening on many occasions during my 8 years in this community. I'm not sure what causes it, but I've seen that it can sometimes trigger that glitch if I've manually added the same fields across different pages, for instance.

 

In such cases, changing the field name doesn't seem to fix the problem either.

 

What has worked for me is to simply delete the fields and then recreate them manually with a different field name. After that, save the document and exit Acrobat.

 

When you reopen Acrobat, go back to the field that you recreated. If you can add the validation script, click OK and remember to save the document. Then, rename the field to the original name and save it again.

 

I know it's a bit of a hassle, but at least it's a relatively quick workaround when everything else has failed.

 

I hope this helps.

matthieu_7164
Participant
April 22, 2026

Video of the error : https://streamable.com/h3d9vv

matthieu_7164
Participant
April 22, 2026

I have that exact same problem. I put validation script in 15 fields, then suddenly, the 16th doesn’t stays selected. It is not due to the code, because even with just a comment, it unselects itself…

Is there a limit of the number of validation scripts in one document ?

Nesa Nurani
Community Expert
Community Expert
April 22, 2026

Are you selecting field from the list? Try selecting field directly by clicking right mouse button and select properties. Also, it may help to not directly jump from field to field why properties are open, close the properties and then select another field.

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?

Legend
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?