Copy link to clipboard
Copied
Hey Everyone,
I'm trying to validate a date field(mm/dd/yyyy) against user input in my Adobe Acorbate form. The date field should only allow the current date, no future/past dates. Here's my code that prevents future / past dates depending on the greater than or less than condition.
if (event.value != "") {
var d = util.scand("mm/dd/yyyy", event.value).getTime();
d = d - (d % 1000);
var nNow = new Date().getTime();
nNow = nNow - (nNow % 1000);
if (d < nNow) {
app.alert("You may not enter a date in the past.");
event.rc = false;}
}
The issue I'm facing is that in each scenario the current date is excluded, it will either have to be a day before today or a day after today. So with the code above for example I cannot enter 05/09/2017 even though it isn't a past date, the code recognizes it as it is. I've tried where d != nNow and that didn't work either.
I'm trying to enforce validation where the user can only enter the current date, hope someone can help.
Thanks!
In that case add the following code to a document level script. Modify the name of the date field in the code to match your field name. The code will add the current date to the field when the document opens if the date is not already populated with a value but will leave it unchanged if there is a value... meaning the user can override the value when the field is editable but not otherwise. When you add the script, the current date will appear in the field. Don't worry, just be sure to follow
...Copy link to clipboard
Copied
Is there a reason that you're not just automatically populating the date in a read-only field? If it always has to be the current date, why are they entering it themselves?
Copy link to clipboard
Copied
I tried implementing this option, but based on the form requirements, the date field is disabled/read only initially. It is only taken out of this state when certain options are selected. I'm not sure if my code was incorrect but I tried implementing this using document level javascript and I got an error stating that the field didn't exist.
If I were to implement this, how would I go about it ?
Copy link to clipboard
Copied
I'd need to know exactly why it becomes editable to provide the right guidance.
Copy link to clipboard
Copied
It becomes editable when this radio button field called Section1.Verification is enabled/checked.
Copy link to clipboard
Copied
So... there is a case where a person using the form may need to override the date value in the field. Correct?
Copy link to clipboard
Copied
Yes, in a sense that particular field won't be editable if the option isn't selected prior.
Copy link to clipboard
Copied
In that case add the following code to a document level script. Modify the name of the date field in the code to match your field name. The code will add the current date to the field when the document opens if the date is not already populated with a value but will leave it unchanged if there is a value... meaning the user can override the value when the field is editable but not otherwise. When you add the script, the current date will appear in the field. Don't worry, just be sure to follow the next instruction.
IMPORTANT! Prior to publishing your form, in the Prepare Form panel, select More, then Reset Form then immediately save the form and close it. Do not pass "Go". Do not collect $200. Just save it and close it. You can now distribute the form.
The script relies on the date field being blank in order to know to populate it with the current date.
var dateField = this.getField("date");
if (dateField.value == "") {
dateField.value = util.printd("mm/dd/yyyy", new Date());
}
Copy link to clipboard
Copied
Thanks Joel, this did work as intended, but partially in my scenario. I have a reset button in my form, that resets all values. This includes the date. So at that point I am back to my issue of having to validate the actual field to only allow current dates.
Copy link to clipboard
Copied
Ok then... Just add the same script I provided to the end of your script to reset the form. If you reset the form using the reset form action, just add another action after that to run a JavaScript and add my code there. In your case, thje code will need to be in both places, document open and in the reset button.
Copy link to clipboard
Copied
Thanks Joel, that worked. Appreciate all the help. Was just curious if there's a way to go about it via javascript validation. As I couldn't get it to work I guess due to the datetime conflicts.
Copy link to clipboard
Copied
You probably could have gotten it to work in the validation step. There's always more than one way to do things. For example, I have frequently used a shoe to drive a nail into the wall to hang a picture frame... it does the trick and if you came over to visit, you wouldn't know which pictures were hung using a shoe and which were hung using a hammer because the scuff marks from the shoe are behind the painting. Code that runs behind the scenes is just like that.
But the nice thing about electronic forms is that we can make the computer do that tedious stuff for us... like knowing the date. Why validate that a user has entered the current date when the computer can do it for them.
BTW: If you need to work with dates frequently in JavaScript, check out my Date library. It extends what the native Date object does to make working with dates much easier.
http://practicalpdf.com/the-practicalpdf-date-library-for-adobe-acrobat/
Find more inspiration, events, and resources on the new Adobe Community
Explore Now