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

Textfield Date validation, disable future dates input

New Here ,
Feb 23, 2017 Feb 23, 2017

Copy link to clipboard

Copied

Hello, I'm creating a form on Acrobat X Pro and I'm trying to restrict the user from entering future dates in a textfield with the date format. Basically the date has to be today's date or prior. I'm a newbie to Acrobat form scripting, so I was wondering how to go about this.

Preferably, I would want to initiate this on field exit if possible, but if not then do it on form submit validation / both. I assume I would have to use a compare operator against the current date such as 'var CurrentDate = new Date();' but I need to know how to associate with the field's value and if any conversion is required, moreover how to initiate this on the exit event.

Additionally, I have a 3 part date field which needs to be validated. It's split with one field for Month, one for Day and one for Year. Is it possible to combine the 3 fields and validate the date for future dates ?

Appreciate any insight on this. Thanks!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

2.4K

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Feb 24, 2017 Feb 24, 2017

Sure, it's quite easy to use. Here's an example:

var dateString = "24/02/2017";

var dateObject = util.scand("dd/mm/yyyy", dateString);

app.alert(dateObject.getTime());

The output is the number of seconds that represent this date (counting from the epoch point, 01/01/1970). This allows you to compare two dates, since its just a number.

Votes

Translate

Translate
Community Expert ,
Feb 24, 2017 Feb 24, 2017

Copy link to clipboard

Copied

You would need to convert the separate strings into a single Date object (using the scand method of the util object) and then compare it to the current date. If it's bigger then you need to decide what to do... Since you used three separate fields it's a more complex situation, though. You can clear them all, or just the last one entered, but that's going to be tricky. Or you could just show an error message but allow the values to remain as they are.

Votes

Translate

Translate

Report

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
New Here ,
Feb 24, 2017 Feb 24, 2017

Copy link to clipboard

Copied

Thanks for your reply try67.

Do you by any chance have an example of using the scand function to achieve this ? I'm ok with a pop up alert and then clearing the values in the 3 fields, as this may be the more straight forward approach.

Votes

Translate

Translate

Report

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 ,
Feb 24, 2017 Feb 24, 2017

Copy link to clipboard

Copied

Sure, it's quite easy to use. Here's an example:

var dateString = "24/02/2017";

var dateObject = util.scand("dd/mm/yyyy", dateString);

app.alert(dateObject.getTime());

The output is the number of seconds that represent this date (counting from the epoch point, 01/01/1970). This allows you to compare two dates, since its just a number.

Votes

Translate

Translate

Report

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
New Here ,
Feb 24, 2017 Feb 24, 2017

Copy link to clipboard

Copied

Appreciate that, I was actually trying to implement something similar(although not sure if correct):

var CurrentDate = new Date();

var DayField = this.getField("txtDay").value;

var MonthField = this.getField("txtMonth").value;

var YearField = this.getField("txtYear").value;

var UserInput = DayField + "/" + MonthField + "/" + YearField; //Not sure if this is the correct way to concatenate the 3 fields.

var DateObject = util.scand("dd/mm/yyyy", UserInput);

if (DateObject > CurrentDate){

app.alert("Cannot enter date in the future");

DayField.value = DayField.defaultValue; // clear the 3 fields to default value which is blank

MonthField.value = MonthField.defaultValue;

YearField.value = YearField.defaultValue;

}

Also, should this be on Validation or on Mouse Up event ?

Votes

Translate

Translate

Report

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 ,
Feb 24, 2017 Feb 24, 2017

Copy link to clipboard

Copied

LATEST

I will place this as the custom calculation script of a (hidden) text field.

Votes

Translate

Translate

Report

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