Skip to main content
Known Participant
May 9, 2017
Answered

Date validation to allow only current dates in date field.

  • May 9, 2017
  • 1 reply
  • 5266 views

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!

This topic has been closed for replies.
Correct answer Joel Geraci

Yes, in a sense that particular field won't be editable if the option isn't selected prior.


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

}

1 reply

Joel Geraci
Community Expert
Community Expert
May 9, 2017

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?

HeliosozAuthor
Known Participant
May 9, 2017

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 ?

Joel Geraci
Community Expert
Community Expert
May 9, 2017

I'd need to know exactly why it becomes editable to provide the right guidance.