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!
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.
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.
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.
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.
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 ?
Copy link to clipboard
Copied
I will place this as the custom calculation script of a (hidden) text field.