Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Date validation: Forms

Community Beginner ,
Oct 09, 2022 Oct 09, 2022

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).

TOPICS
JavaScript , PDF forms
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
3 ACCEPTED SOLUTIONS
Community Expert ,
Oct 09, 2022 Oct 09, 2022

In this script you use only the field "DateToday".

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2022 Oct 10, 2022

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");

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 10, 2022 Oct 10, 2022

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.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2022 Oct 09, 2022

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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2022 Oct 09, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 09, 2022 Oct 09, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 09, 2022 Oct 09, 2022

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");
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2022 Oct 10, 2022

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");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 10, 2022 Oct 10, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2022 Oct 10, 2022

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");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 10, 2022 Oct 10, 2022

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 = "";
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2022 Oct 10, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 10, 2022 Oct 10, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 10, 2022 Oct 10, 2022
LATEST

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");
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2022 Oct 09, 2022

In this script you use only the field "DateToday".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines