These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Date range test

Runolfr
Registered: Oct 1 2008
Posts: 119

I need to verify that a date of birth submitted in a date field isn't a future date. I'm guessing that would go in the Validate event for the control, but what code do I use to make the form's ExecValidate event return false if the date entered isn't acceptable?

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes, the validate event is the correct place, or at least one of them depending on what you want to do.
This is quite simple. See this article on handling dates.

http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/date_time_part1/

The JavaScript Date Object has a function that returns the millisecond value of the date/time, "getTime()". This is the key value of the DateObject. So all you have to do is compare the millisecond values of the entered date with the current date.

Of course the first thing that needs to be done is parsing the entered date text into a date object. You'll find this in Part 2 of the article mentioned above.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Runolfr
Registered: Oct 1 2008
Posts: 119
So I'm thinking I need an if statement...

// DateOfBirth is invalid if the value is a future date
var currDate = new Date();
if (this.getTime() > currDate.getTime())
{
// what to do here?
}

Assuming I've got the condition right, I can't remember how to cause the result to be "invalid"
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You've got this partially right. But you have to parse the input date text to get a date object. There is a function for this in Acrobat's Util Object "util.scand". It's discussed in Part 2 of the article I mentioned above.

I used to have an article on JavaScript corner on the validate event but it seems to have gone missing:(

The return value from the validate event is set with "event.rc". Set it to true or false.

Detailed (and I mean really detailed) infomation on all the Acrobat scripting events can be found in the many video tutorials at www.pdfscripting.com

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Runolfr
Registered: Oct 1 2008
Posts: 119
I'll have to check with the VP of IT about subscribing to that tutorial site.

In the meantime, I've come up with the following code:

// DateOfBirth is invalid if the value is not a past date
var currDate = new Date();
var birthDate = util.scand("mmm d, yyyy", this.rawValue);
if (birthDate.getTime() >= currDate.getTime())
event.rc = false;

While this seems to run without any errors, it doesn't actually generate an "invalid" response if the birthDate is equal to or later than the currDate.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Ahh, sorry I assumed that you were using an AcroForm. After looking back at your message I believe that you are probably using a LiveCyle form.

Is this true? This is critical information for answering questions.

If it is then event.rc won't work, or won't work exactly. You'll need to set the last value evaluated by the script to either true or false. The easiest way to do this is by setting a variable to the validate value, then placing this variable on it's own line at the end of the script.

for example:
var bVal = true; if(...)bVal = false; bVal;

However, your code already does this. Even though event.rc is not valid for an LC form, it is the last line of the script when the Invalid Date conditions exist. So there is something else wrong.

Place the if in a block and add a "console.println()" statement to it. That way you'll know if the condition is ever really triggered. You should also use a "console.println()" to test whether or not the "birthDate" is parsed correctly.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Runolfr
Registered: Oct 1 2008
Posts: 119
Sorry for the confusion, this is definitely a LiveCycle Designer form.

There's no sign that the code is running, since I don't get anything from the "console.println()" statements.

console.println("Validating DOB");

// DateOfBirth is invalid if the value is not a past date
var valState= true;
var currDate = new Date();
var birthDate = util.scand("mmm d, yyyy", this.rawValue);
console.println(birthDate.toString());
if (birthDate.getTime() >= currDate.getTime())
valState= false;

valState;
Runolfr
Registered: Oct 1 2008
Posts: 119
If I tell it to throw up an alert in the Validate event, it does when I change the value of the DateOfBirth field.

// Verify execution
var cMsg = "Are you seeing this pop-up? \n\n";
var nRtn = app.alert(cMsg,1,2, "Yes/No?");

// DateOfBirth is invalid if the value is not a past date
var valState= true;
var currDate = new Date();
var birthDate = util.scand("mmm d, yyyy", this.rawValue);
console.println(birthDate.toString());
if (birthDate.getTime() >= currDate.getTime())
valState= false;

valState;
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Your code looks good, if the alert is being displayed then something should be being displayed in the console window. It's possible that the date parse isn't working and returning an empty string. In that case it'd be hard to see if anything was happening in the console window.

Change the "console.println()" statement to:

console.println("Date: " + birthDate.toString());

And keep the console window open while you are entering a value into the form so you can see the text as it happens. In fact, clear the console before you start.

BTW: You should probably test for the case of an invalide date string as well as a past date value.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script