Skip to main content
Participant
September 2, 2022
Answered

Change text color in multiple fields based on expired date.

  • September 2, 2022
  • 2 replies
  • 846 views

Hi friends,

 

Im in Acrobat Pro DC and I have multiple fields that I want to turn the text red if the "Expires" cell is any day before today.Expired.

I have a field that already calculates the current date from a Document level script; Today_af_date. Here is that code: 

function dateToday() {
var d = new Date();
f = this.getField("Today_af_date");
f.value = d;
}

dateToday();

 

Here is the code that is in Custom Val Script: of the "Expires" cell - NOTE: I have 52 fields that I need this entered into. Should it be a Document level script? 

 

I'm getting a Syntax error 19 at line 20, as well?

 

var Today = this.getField("Today_af_date").value;
var Expired = event.value;

 

// Check to see if the cells are filled with data
if(Today.length || Expired.length) {

 

// Make sure date values are the same
var dateToday = util.scand("mm/dd/yy", Today);
var dateExpired = util.scad("mm/dd/yy",Expired);

 

// Fire off the validation
if (dateExpired.value<=dateToday.value);{
event.target.textColor = color.red;
this.getField("docLicense.0").textColor = color.red;
this.getField("docLicenseNumber.0").textColor = color.red;
this.getField("docIssueDate.0").textColor = color.red;}

 

// Change them back to black if the Expired Date is changed to Non-Expired
else{
event.target.textColor = color.black;
this.getField("docLicense.0").textColor = color.black;
this.getField("docLicenseNumber.0").textColor = color.black;
this.getField("docIssueDate.0").textColor = color.black;}

}

This topic has been closed for replies.
Correct answer Bernd Alheit

At

 if (dateExpired.value<=dateToday.value);{

remove the ; and .value

2 replies

bebarth
Community Expert
Community Expert
September 3, 2022

Hi,

Please have a look at the attached file.

In calculate script of the Today_af_date field:

event.value=util.printd("mm/dd/yy",new Date());

and in calculate script of the Expires_af_date field:

if (event.value<=this.getField("Today_af_date").value) {
	this.getField("docLicense.0").textColor=color.red;
	this.getField("docLicenseNumber.0").textColor=color.red;
	this.getField("docIssueDate.0").textColor=color.red;
} else {
	this.getField("docLicense.0").textColor=color.black;
	this.getField("docLicenseNumber.0").textColor=color.black;
	this.getField("docIssueDate.0").textColor=color.black;
}

I didn't understand about your 52 fields!

Dou you have to change the font color of 52 fields if the expires date is before today?

@+

 

Participant
September 7, 2022

Thank you for both responces, 

Turns out that I did have a syntax error ( Thank you @Bernd Alheit )

I ended up using my original script with the corrections. 

The simplified script by @bebarth does not activate if the user inputs the date manually. It will run on the first input, but if we want to change the date the script does not run. The colors do not change back to black.

Also, I wanted the target field to change as well. 

 

So, this is what I went with: (this runs no matter the input - manual or selecting the date from the calendar) 

 

var Today = this.getField("Today_af_date").value;
var Expired = event.value;

 

// Check to see if the cells are filled with data
if(Today.length || Expired.length) {

 

// Make sure date values are the same
var dateToday = util.scand("mm/dd/yy", Today);
var dateExpired = util.scand("mm/dd/yy",Expired);

 

// Fire off the calculation
if (dateExpired<=dateToday){
event.target.textColor = color.red;
this.getField("docLicenses.1").textColor = color.red;
this.getField("docLicenseNumber.1").textColor = color.red;
this.getField("docLicenseIssueDate.1").textColor = color.red;}

 

// Change them back to black if the Expired Date is changed to Non-Expired
else{
event.target.textColor = color.black;
this.getField("docLicenses.1").textColor = color.black;
this.getField("docLicenseNumber.1").textColor = color.black;
this.getField("docLicenseIssueDate.1").textColor = color.black;}

}

 

Now I have 52 more sets of these fields to ads this script.

 

Thank you both for the help!

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
September 3, 2022

At

 if (dateExpired.value<=dateToday.value);{

remove the ; and .value