Javascript help with Date validation for pdf form.
Copy link to clipboard
Copied
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;
}
}
}
Copy link to clipboard
Copied
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;}}}}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Hi Nesa,
thanks again. If the date is newer, prefer it to be discarded however if it's older than 30 months can we have the alert as setup now and also that the value shouldn't be discarded but the field getting highlighted in yellow.
Thanks..
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi Nesa,
thanks again for the advise so far. I've got several validation scripts in the form for checking users response to combo box (mostly yes/no) which change the background colour of the combo box accordingly. I have also added a submit button which attaches the pdf form to an e-mail which the user can send. I want to add another push button for 'validating' the data in various combo boxes (yes/no) and text boxes (dates in dd/mm/yyyy format). The reason is if the user fills out this form in a pdf software which doesn't support JS, and submits the form to us I'm afraid that some boxes may not get highlighted in different colours when we receive them. Thus one 'validate' push button would allow us to get an app alert based on the users submission and check the responses which need our attention. Can you advise if a script is possible for this 'validate' push button to check several fields and give me a combined app alert.
eg.
- If combobox1 (Yes/No/" ") is marked 'No' then app alert to include "Comb box 1 is marked no - please check", if yes/" " then no app alert required.
- If combobox2 (Yes/No/" ") is marked 'No' then app alert to include "Comb box 2 is marked no - please check". if yes/" " then no app alert required.
- if text3 (numerical value) is more than 5, then app alert to include "value in text 3 is higher than 5- please check". If within 5 or less then no alert required.
- If date in text33 (dd/mm/yyyy) is more than 30 months old from date in text 21 (did/mm/yyyy HH:MM) the app alert to include "Date in text 33 is more than 30 months from date in text 21 - please check". If data is within 30 months then no alert required.
- if checkbox1 is not checked then app alert to include "checkbox1 is not checked - please check'. If checked then no alert required.
the list of app alert message should ignore the field if the data is within the desired parameter else give a combined popup app alert for all fields that require checking. If I could get a code to do the above I will then try to add similar additio a; checks in the code for each textbox/combobox/checkbox check I need. Hope you can help. Many thanks.
Copy link to clipboard
Copied
Thanks Nesa, I don't have my individual scripts in the calculations. It's only added to the individual validation tab.
I would prefer your proposed second option for a modified script to work from a button. see my next reply to your messag for explanation for what i need. Hope you can help and really appreciate your helpo with this.
Copy link to clipboard
Copied
Here is example script for "Combobox1", "Combobox2" and "Text3" fields:
var c1 = this.getField("Combobox1").valueAsString;
var c2 = this.getField("Combobox2").valueAsString;
var t3 = Number(this.getField("Text3").valueAsString);
var alert = [];
if(c1 == "No")
alert.push("Combobox 1 is marked no - please check.");
if(c2 == "No")
alert.push("Combobox 2 is marked no - please check.");
if(t3 > 5)
alert.push("Value in Text3 is higher than 5 - please check.");
if(alert.length !== 0)
app.alert(alert.join("\n"));
else
app.alert("No action required.",3);
Copy link to clipboard
Copied
Hi Nesa,
Many thanks again. This works very well for the validation button. I have further embedded Text33 date check into this as well. The problem is that even if the field in Text24c is left blank I get the push alert ""Please recheck date in Text24c - Cannot be prior to 06 months from the date in date in Text21." Have copied the script below.
1. Can you advise how to modify this further if I want the alert to be included only if a date is entered in Text 24c AND the date is more than 6 monhs from Text21 date, and be ignored if within 6 months from Text21 and also ignored if no date is entered. For now, the alert is included if no date is entered as well which i wish to remove.
2. Can you also advise how to add a push alert for a check box? checkbox1 is not checked then app alert to include "checkbox1 is not checked - please check'. Have tried a few things but they dont work.
var c1 = this.getField("Combobox1").valueAsString;
var c2 = this.getField("Combobox2").valueAsString;
var t3 = Number(this.getField("Text3").valueAsString);
var startDateString = this.getField("Text21").valueAsString;
var startDate = util.scand("dd/mm/yyyy", startDateString);
var endDateString1 = this.getField("Text24c").valueAsString;
var endDate1 = util.scand("dd/mm/yyyy", endDateString1);
var month06 = new Date(startDate);
month06.setMonth(month06.getMonth() - 06)
var alert = [];
if(c1 == "No")
alert.push("Combobox 1 is marked no - please check.");
if(c2 == "No")
alert.push("Combobox 2 is marked no - please check.");
if(t3 > 5)
alert.push("Value in Text3 is higher than 5 - please check.");
if (startDateString === "")
alert.push ("You must first enter Date in Text21");
if (endDate1.getTime() > startDate.getTime())
alert.push ("Please recheck date in Text24c - Cannot be later than the date in date in Text21");
if (endDate1.getTime() < month06.getTime())
alert.push ("Please recheck date in Text24c - Cannot be prior to 06 months from the date in date in Text21.");
if(alert.length !== 0)
app.alert(alert.join("\n"));
else
app.alert("No action required.",3);
Copy link to clipboard
Copied
Add condition to check that "Text24c" is not empty: endDateString1 !== ""
if (endDateString1 !== "" && endDate1.getTime() > startDate.getTime())
Do it for both lines where you compare endDate1.
For checkbox1 add this condition:
if(this.getField("checkbox1").valueAsString === "Off")
alert.push("checkbox1 is not checked - please check");
Copy link to clipboard
Copied
For older than 30 months, replace event.rc = false with event.target.fillColor = color.yellow;

