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 5, 2024

One more question. If a user fills up this form with a non JavaScript supported pdf program and submits to us, believe the user won't get any alerts or change in colours).  can we add a validate button at the end so when we receive the form and press validate, all the individual fields (textboxes and combo boxes) with JS can execute and possibly give a report of what the user alerts would be and highlight(change background colour) of fields which get triggered on the basis of their date comparison or response? 

thanks


If you have all calculation scripts you could try using this.calculateNow() in a button to trigger all calculations, otherwise it may be possible if you put all scripts slightly changed to work from a button.