Expand my Community achievements bar.

SOLVED

Limit Date Fields to Only Allow Past or Future Dates?

Avatar

Former Community Member

Hi,

I am using LiveCycle ES3.  My request is simple.  I have multiple date fields throughout my form.  Some date fields should only allow past dates; Some date fields should only allow future dates.  When an invalid date is entered, the date should be rejected, the field remain required, and an error message pop up, saying "This date cannot be in the future" or "This date cannot be in the past", depending on which field it is. 

As a note, I used the exact script from the demo file "Highlighting Required Fields", however I discovered that this script is overly complicated and even worse still it is incorrect.

Any help would be greatly appreciated.

Thank you.

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

Hi,

could you please post a link to "Highlighting Required Fields"?

This code is doing what you are asking for:

var d = this.rawValue;

var dArr = d.split("-");

var d1 = new Date(dArr[0], dArr[1]-1, dArr[2]);

var dNow = new Date();

var diff1 = Math.floor((dNow.getTime() - d1.getTime())/86400000);

if (diff1>0) {

          xfa.host.messageBox("past" + diff1);

}

var diff2 = Math.ceil((d1.getTime() - dNow.getTime())/86400000);

if (diff2>0) {

          xfa.host.messageBox("future" + diff2);

}

Put it into the Validate event.

Also set the Validation Pattern... to date{D MMMM YYYY}

in both Display | Edit | Validation tab

This is how I tested it but you should be allowed to choose whatever Validation Pattern you like.

Hope it helps.

View solution in original post

6 Replies

Avatar

Correct answer by
Former Community Member

Hi,

could you please post a link to "Highlighting Required Fields"?

This code is doing what you are asking for:

var d = this.rawValue;

var dArr = d.split("-");

var d1 = new Date(dArr[0], dArr[1]-1, dArr[2]);

var dNow = new Date();

var diff1 = Math.floor((dNow.getTime() - d1.getTime())/86400000);

if (diff1>0) {

          xfa.host.messageBox("past" + diff1);

}

var diff2 = Math.ceil((d1.getTime() - dNow.getTime())/86400000);

if (diff2>0) {

          xfa.host.messageBox("future" + diff2);

}

Put it into the Validate event.

Also set the Validation Pattern... to date{D MMMM YYYY}

in both Display | Edit | Validation tab

This is how I tested it but you should be allowed to choose whatever Validation Pattern you like.

Hope it helps.

Avatar

Former Community Member

Okay, I know it's been a while since this post, but I discovered an issue with this script when put into practice.

This script is in the validate event of a field within a form that a client submits to us.  Having error messages pop up when the client is entering invalid dates is great because it's intended to prompt them to enter a valid date.  However, if they ignore the message and enter an invalid date anyway, then when they send the form back to us, WE see the error message every single time the form is opened.  Additionally, if they did enter a valid date, but the field is scripted to not allow past dates, if the form is opened up in the future, based on today's date (the term "Now" in the above script), well, bam, the date is now invalid as when compared to today it is a past date.  This is not desirable.  Why is the form validating this field every time the form is opened?  I want it to only validate the field when the field is specifically entered and exited.  Do I simply move the script above from the validate event to the exit event to achieve this?

Thank you.

Avatar

Level 10

To fix this you should have a hidden field somewhere in your form, when the form is opened or some data is entered, enter the today's date in this field (if the field's value is null) and then use this field's value to compare the dates entered by the users...

If you want to get rid of the message every time it is opened I suggest you to write the code in the exit event, so each time they put an incorrect value they are prompted a message regarding the value entered.

Avatar

Level 3

I have a date field validated to only show the month and year (MMYY). How would I restrict a user from entered any date prior to 0114?

Avatar

Level 10

Hi there,

I've done something similar to this but it might not be the best method so far...

What I've done is I have a textfield in which users enter the date in the format wanted..

The value entered is than transfered to a datetimefield which automatically converts the value 11/14 to 01/11/14

from there you can work around with the dates like you want...

Hope this help!

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----