Skip to main content
Inspiring
October 22, 2025
Answered

Script to highlight empty required fields

  • October 22, 2025
  • 2 replies
  • 344 views

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});
Correct answer Przemyslaw33443682rriu

Use document actions ⇾ Document Will Close.


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"
    });
}

2 replies

Inspiring
October 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

Nesa Nurani
Community Expert
Community Expert
October 24, 2025

Use document actions ⇾ Document Will Close.

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

Inspiring
October 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

Nesa Nurani
Community Expert
Community Expert
October 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.