Copy link to clipboard
Copied
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});
Copy link to clipboard
Copied
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"
});
}
Copy link to clipboard
Copied
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 });
}
f.fillColor sets the background colour of the field.https://adobe.ly/47dRTJf, color.yellow, color.white, or create custom colours with color.rgb(r,g,b).
Also, wait for more inputs from experts.
Best regards,
Tariq | Adobe Community Team
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I think that Nesa is correct, you must have field highlighting turned on. Add this to the top of your script.
app.runtimeHighlight = false;
Copy link to clipboard
Copied
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 });
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
@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
Copy link to clipboard
Copied
Use document actions ⇾ Document Will Close.
Copy link to clipboard
Copied
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"
});
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now