Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

Script to highlight empty required fields

Explorer ,
Oct 22, 2025 Oct 22, 2025

Hi, I have button with script to valdiate required fields (fields, boxes, radio). Script showing alert with listed empty fields. I wish if it possible to change colour (fill; background) of fields/radio boxes which is still empty?

var emptyFields = [];
for (var i=0; i<this.numFields; i++) {
     var f= this.getField(this.getNthFieldName(i));
     if (f.type!="button" && f.required ) {
          if (f.valueAsString==f.defaultValue) emptyFields.push(f.name);
     }
}
if (emptyFields.length>0) {
     app.alert({cMsg:"Incomplete! Missing fields:\n" + emptyFields.join("\n"), nIcon:1,});
} else app.alert({cMsg:"All is good", nIcon:4});
TOPICS
Create PDFs , JavaScript , PDF , PDF forms
265
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Explorer ,
Oct 27, 2025 Oct 27, 2025
LATEST

For everybody who shearch for solution, below code and instruction:

Highlighting fields before document is closing
Follow patch: Tools -> JavaScript -> Document Actions -> Document Will Close (edit with app.runtimeHighlight = true;)

Script for highlight empty required fields

var emptyFields = [];
app.runtimeHighlight = false;

// RESET all colors before checking
for (var i = 0; i < this.numFields; i++) {
    var f = this.getField(this.getNthFieldName(i));
    if (f && f.type != "button") {
        f.fillColor = color.transparent;
    }
}

// Check empty fields
for (var i = 0; i < this.numFields; i++) {
    var fieldName = this.getNthFieldName(i);
    var f = this.getField(fieldName);
    
    if (f && f.type != "button" && f.required) {
        if (f.valueAsString == "" || f.valueAsString == f.defaultValue || f.valueAsString == null) {
            emptyFields.push(fieldName);
            f.fillColor = color.red;
        }
    }
}

if (emptyFields.length > 0) {
    app.alert({
        cMsg: "Fill this red fields:\n" + emptyFields.join("\n"),
        nIcon: 1,
        cTitle: "Fields checking"
    });
} else {
    app.alert({ 
        cMsg: "All is good", 
        nIcon: 3,
        cTitle: "Fields checking"
    });
}

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Oct 22, 2025 Oct 22, 2025

Hello, 

 

Yes, you can highlight the empty fields by changing their background colour dynamically in your validation script. Acrobat JavaScript allows you to modify field properties such as fillColor. Here’s how you can extend your existing code:

 

var emptyFields = [];
for (var i = 0; i < this.numFields; i++) {
    var f = this.getField(this.getNthFieldName(i));
    if (f.type != "button" && f.required) {
        if (f.valueAsString == f.defaultValue) {
            emptyFields.push(https://adobe.ly/475LiQZ);
            // Change background colour to light yellow for empty fields
            f.fillColor = color.yellow;
        } else {
            // Reset colour for filled fields
            f.fillColor = color.white;
        }
    }
}

if (emptyFields.length > 0) {
    app.alert({
        cMsg: "Incomplete! Missing fields:\n" + emptyFields.join("\n"),
        nIcon: 1
    });
} else {
    app.alert({ cMsg: "All is good", nIcon: 4 });
}

Key Points:

  • f.fillColor sets the background colour of the field.
  • Use predefined colour constants like https://adobe.ly/47dRTJf, color.yellow, color.white, or create custom colours with color.rgb(r,g,b).
  • Resetting colour for filled fields ensures the form looks normal once completed.


Also, wait for more inputs from experts.

Best regards,
Tariq | Adobe Community Team

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 22, 2025 Oct 22, 2025

Tariq Ahmad, thank you, unfortunately this code not working well.

I don't get this links adobe.ly - what insert in this place? Inserting colors don't work correctly.

This script only changing background color when I focus (mouse up) to one of required fields

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2025 Oct 22, 2025

Do you wish to change the fill color of the fields that are still empty automatically or when you push the button?
If you have fields highlight turned on, you won't be able to see background color.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2025 Oct 22, 2025

I think that Nesa is correct, you must have field highlighting turned on.  Add this to the top of your script.

 

app.runtimeHighlight = false;

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 24, 2025 Oct 24, 2025

Nesa, Thom, thank you, highlighting was the problem.

Next problem is with repeating validation button script, explanation:
Fill some fields -> Click button -> Empty fields turn red -> Fill this red fields -> Click button -> Filled fields is still red

BTW: Is posibble to app.runtimeHighlight = true; automaticly before closing document? I found some beforeClose event solutions but I don't get it.

My actual script:

var emptyFields = [];
app.runtimeHighlight = false;
for (var i = 0; i < this.numFields; i++) {
    var f = this.getField(this.getNthFieldName(i));
    if (f.type != "button" && f.required) {
        if (f.valueAsString == f.defaultValue) {
            emptyFields.push(f.name);
            // Change background colour to red for empty fields
            f.fillColor = color.red;
        } else {
            // Reset colour for filled fields
            f.fillColor = color.transparent;
        }
    }
}

if (emptyFields.length > 0) {
    app.alert({
        cMsg: "Please fill empty fields:\n" + emptyFields.join("\n"),
        nIcon: 1,
	  cTitle: "Fields checking"
    });
} else {
    app.alert({ cMsg: "All is good!", nIcon: 4 });
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 24, 2025 Oct 24, 2025

 

The reason your filled fields stay red is that the color isn’t being reset correctly between runs. Try changing the reset color to color.white instead of color.transparent, since transparent can behave unpredictably depending on field highlighting or layer visibility.

Here’s the small tweak:

if (f.valueAsString == f.defaultValue) {
    f.fillColor = color.red;
    emptyFields.push(f.name);
} else {
    f.fillColor = color.white; // use white instead of transparent
}

 

As for restoring app.runtimeHighlight = true before closing the document — yes, that’s possible by adding a Document Will Close action under File > Properties > Actions. Just set the JavaScript to:

app.runtimeHighlight = true;

  

That should fix both issues and make the script behave consistently.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 24, 2025 Oct 24, 2025

@Jim_Rick6879 unfortunately changing transparent to white don't fix the issue...

About app.runtimeHighlight please tell me how to insert this information, I follow path but don't see actions only this but didn't work
Screen.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 24, 2025 Oct 24, 2025

Use document actions ⇾ Document Will Close.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 27, 2025 Oct 27, 2025
LATEST

For everybody who shearch for solution, below code and instruction:

Highlighting fields before document is closing
Follow patch: Tools -> JavaScript -> Document Actions -> Document Will Close (edit with app.runtimeHighlight = true;)

Script for highlight empty required fields

var emptyFields = [];
app.runtimeHighlight = false;

// RESET all colors before checking
for (var i = 0; i < this.numFields; i++) {
    var f = this.getField(this.getNthFieldName(i));
    if (f && f.type != "button") {
        f.fillColor = color.transparent;
    }
}

// Check empty fields
for (var i = 0; i < this.numFields; i++) {
    var fieldName = this.getNthFieldName(i);
    var f = this.getField(fieldName);
    
    if (f && f.type != "button" && f.required) {
        if (f.valueAsString == "" || f.valueAsString == f.defaultValue || f.valueAsString == null) {
            emptyFields.push(fieldName);
            f.fillColor = color.red;
        }
    }
}

if (emptyFields.length > 0) {
    app.alert({
        cMsg: "Fill this red fields:\n" + emptyFields.join("\n"),
        nIcon: 1,
        cTitle: "Fields checking"
    });
} else {
    app.alert({ 
        cMsg: "All is good", 
        nIcon: 3,
        cTitle: "Fields checking"
    });
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines