Copy link to clipboard
Copied
I want to validate "StartDate" by testing if it is greater than "DateToday" on an application form that I created on Adobe Forms. If the "StartDate" is greater than "DateToday", I want a message to appear that the "StartDate" is invalid.
Please assist with the Javascript for this as an Action or Validation (not sure which is the correct one to use, so please advise).
Copy link to clipboard
Copied
In this script you use only the field "DateToday".
Copy link to clipboard
Copied
Use this as calculation script:
var d1 = util.scand("dd-mmm-yyyy",this.getField("StartDate").value);
var d2 = util.scand("dd-mmm-yyyy",this.getField("DateToday").value);
if (d1.getTime() > d2.getTime())
app.alert("Start date may not be in history");
Copy link to clipboard
Copied
APologies,
No error message appears. The StartDate field is not cleared. The StartDate value is entered by means of the date picker on Adobe forms. The DateToday field is a calculated field with this script:
var f = this.getField("DateToday");
f.value = util.printd("dd-mmm-yyyy", new Date());
This might also form part of the script relating to the "if statement", I think, and does not have to be a seperate form field. It is only used for the calculation in the validation of the "StartDate" field.
Copy link to clipboard
Copied
Hi @Houdini_F
Since your question is about Acrobat Forms, I have moved this from the Using the Community forum where you posted to Acrobat and edited your post to add "Forms" and "JavaScript" as the tags.
~ Jane
Copy link to clipboard
Copied
Since you have two fields and can't know which one will be filled first, I would suggest to use calculation script, but then again if "DateToday" will always have today's date you don't need it in script, you can just compare "StartDate" to a variable containing new date() in that case I would use Validation script in "StartDate" field.
There are lots of scripts on this forum for comparing two dates.
Copy link to clipboard
Copied
Hi there and thanks for the advice.
This is the script I have put together, but I can't get it to work.
var dd1 = this.getField("DateToday");
dd1.value = util.printd("dd-mmm-yyyy", new Date());
var dd2 = this.getField("DateToday");
var dd1 = util.scand("dd-mmm-yyyy", dd1.valueAsString);
var dd2 = util.scand("dd-mmm-yyyy", dd1.valueAsString);
if(dd2>dd1)
(
result="Contract start date not Valid"
)
Please assist.
Copy link to clipboard
Copied
I also attempted it this way:
var d1 = this.getField("StartDate").value;
var d2 = this.getField("DateToday").value;
{
if (d1 > d2)
app.alert("Start date may not be in history");
}
Copy link to clipboard
Copied
Use this as calculation script:
var d1 = util.scand("dd-mmm-yyyy",this.getField("StartDate").value);
var d2 = util.scand("dd-mmm-yyyy",this.getField("DateToday").value);
if (d1.getTime() > d2.getTime())
app.alert("Start date may not be in history");
Copy link to clipboard
Copied
Great Thanks
I would also like to add to the script to clear the entry in the "StartDate" field, how do I do that, please.
Copy link to clipboard
Copied
Change this:
if (d1.getTime() > d2.getTime())
app.alert("Start date may not be in history");
To:
if (d1.getTime() > d2.getTime()) {
this.getField("StartDate").value = "";
app.alert("Start date may not be in history");
}
Copy link to clipboard
Copied
It does not work, unfortunately. This is what the script is:
var d1 = util.scand("dd-mmm-yyyy",this.getField("StartDate").value);
var d2 = util.scand("dd-mmm-yyyy",this.getField("DateToday").value);
if (d2.getTime() > d1.getTime()){
app.alert("Start date may not be in history");
this.getField("StartDate").value = "";
}
Copy link to clipboard
Copied
Saying "it does not work" is not very helpful... What exactly goes wrong? Is there an error message of some kind in the JS Console when you use it? What are the values of the fields when you run it? etc.
Copy link to clipboard
Copied
APologies,
No error message appears. The StartDate field is not cleared. The StartDate value is entered by means of the date picker on Adobe forms. The DateToday field is a calculated field with this script:
var f = this.getField("DateToday");
f.value = util.printd("dd-mmm-yyyy", new Date());
This might also form part of the script relating to the "if statement", I think, and does not have to be a seperate form field. It is only used for the calculation in the validation of the "StartDate" field.
Copy link to clipboard
Copied
At last, this it what worked:
var d1 = util.scand("dd-mmm-yyyy",this.getField("StartDate").value);
var d2 = util.scand("dd-mmm-yyyy",this.getField("DateToday").value);
if (d1.getTime() < d2.getTime())
{
app.alert("Start date may not be in history")
this.resetForm("StartDate");
}
Copy link to clipboard
Copied
In this script you use only the field "DateToday".

