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

Date validation to allow only current dates in date field.

New Here ,
May 09, 2017 May 09, 2017

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!

TOPICS
Acrobat SDK and JavaScript , Windows
4.8K
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

correct answers 1 Correct answer

Community Expert , May 09, 2017 May 09, 2017

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

...
Translate
Community Expert ,
May 09, 2017 May 09, 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?

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
New Here ,
May 09, 2017 May 09, 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 ?

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 ,
May 09, 2017 May 09, 2017

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

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
New Here ,
May 09, 2017 May 09, 2017

It becomes editable when this radio button field called Section1.Verification is enabled/checked.

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 ,
May 09, 2017 May 09, 2017

So... there is a case where a person using the form may need to override the date value in the field. Correct?

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
New Here ,
May 09, 2017 May 09, 2017

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

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 ,
May 09, 2017 May 09, 2017

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

}

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
New Here ,
May 09, 2017 May 09, 2017

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.

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 ,
May 09, 2017 May 09, 2017

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.

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
New Here ,
May 09, 2017 May 09, 2017

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.

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 ,
May 09, 2017 May 09, 2017
LATEST

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/

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