Skip to main content
Participating Frequently
January 4, 2024
Question

Javascript help with Date validation for pdf form.

  • January 4, 2024
  • 1 reply
  • 2730 views

Hi I have created a fillable pdf form. 

 

"Text21" is a field in 'dd/mm/yyyy HH:MM' format. 

 

I need a script for textbox "Text33" which is in dd/mm/yyyy format which should alert the user if the date entered in "Text33" is more than 30 months old from the date entered in "Text21". Also a seperate alert if the date entered is newer than "Text21".

 

Im not good at JS but have come up with the following from forums, though this just checks that field 21 needs to be filled first and then only if the date is newer than the "Text21" date.


Can anyone please guide or assist with the script? Thanks.

 

if (event.value) {
var startDateString = this.getField("Text21").valueAsString;
if (startDateString=="") {
app.alert("You must first enter Field 21");
event.rc = false;
} else {
var startDate = util.scand("dd/mm/yyyy", startDateString);
var endDate = util.scand("dd/mm/yyyy", event.value);
if (endDate.getTime()  < startDate.getTime()) {
app.alert("Please recheck date - Must be within 30 months from Date of field 21");
event.rc = false;
}
}
}

This topic has been closed for replies.

1 reply

Nesa Nurani
Community Expert
Community Expert
January 4, 2024

Try this:

if (event.value) {
 var startDateString = this.getField("Text21").valueAsString;

 if (startDateString === "") {
  app.alert("You must first enter Field 21");
  event.rc = false;} 
 else {
  var startDate = util.scand("dd/mm/yyyy", startDateString);
  var endDate = util.scand("dd/mm/yyyy", event.value);

  if (endDate.getTime() > startDate.getTime()) {
   app.alert("Please recheck date - End date cannot be after start date");
   event.rc = false;} 
  else {
   var month30 = new Date(startDate);
   month30.setMonth(month30.getMonth() - 30);

   if (endDate.getTime() < month30.getTime()) {
    app.alert("Please recheck date - End date cannot be more than 30 months from start date");
    event.rc = false;}}}}
Participating Frequently
January 4, 2024

Hi Nesa,

 

thanks this works great. Is it possible to highlight or change the background colour of this field to yellow if the date is newer or more than 30 months from the date in "Text21". Many thanks. 

Nesa Nurani
Community Expert
Community Expert
January 4, 2024

To show background color, you would have to turn off fields highlight in your preferences.

Changing background color wouldn't make much sense, since the script will discard the value if the date is newer or older than 30 months. Unless you want to replace discarding value with background color?