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

HELP comparing dates using JS validation

hfelsh
Registered: Dec 21 2007
Posts: 9

I've been scouring the 'net (and these boards) for days now. [img]http://www.theamazingcams.com/animated_icons/book.gif[/img] I cannot, for the life of me, figure out this problem. Anyone who can help - I'd appreciate it!

Here's the scenario:

I have a button, which is called "Setdate". It uses JS to fill in the current date to the field "Today" (and this works correctly.)

this.getField("Today").value = util.printd("mm/dd/yyyy", new Date());
I then have two date fields, called "Spec1OLD" and "Spec2OLD", respectively. I need to compare the date the user enters in those fields, against the current date (in the field "Today"), and if the date the user enters is LESS than today's date, have Acrobat throw an error dialog; if it's equal to or greater, move to the next field (called "Spec1OLDstartTime" or "Spec2OLDstartTime".) I also only want the code validation to happen if both fields have a value (if the Today field is blank, the validation should not happen.)

This is the code I am using for the respective fields:

Spec1OLD / Spec2OLD (names are changed in my PDF, but the code is the same)

var strA = util.scand("mm/dd/yyyy", this.getField("Today").value);
var strB = util.scand("mm/dd/yyyy", this.getField("Spec1OLD").value);
 
if (strB.length || strA.length)
  if (strB > strA)
   app.alert({cMsg:"day\/date cannot be less than today\'s date. Please choose a date at least 48 hours from today\'s date.",nIcon:1});
  else{
   if (strB <= strA)
   if ( event.fieldFull || event.willCommit )
   this.getField("Spec1OLDstartTime").setFocus();
}

Unfortunately, I cannot, for the life of me, get this to work. [img]http://tampabaymiatas.net/forum/images/smiles/eusa_wall.gif[/img]

Can anyone provide any insight as to what I'm doing wrong?

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
You can not compare the lengths of the date time object you need to compare the value of the date time objects using the "valueOf()" method to get the value. Also, the length of the date time object is undefined in JavaScript. Try the following code. You will still need to add code to resolve the "48 hour" requirement.


var strA = util.scand("mm/dd/yyyy", this.getField("Today").value);
var strB = util.scand("mm/dd/yyyy", this.getField("Spec1OLD").value);

// some added debugging code for information
console.show()
console.println("Today:")
console.println("entry: " + strA);
console.println("length: " + strA.length);
console.println("vlaue: " + strA.valueOf() );

console.println("date:")
console.println("entry: " + strB);
console.println("length: " + strB.length);
console.println("vlaue: " + strB.valueOf() );
console.println("B > A: " + ( strB.valueOf() > strA.valueOf() ) );
// end debugging code

if ( strB.valueOf() < strA.valueOf() )
app.alert({cMsg:"day\/date cannot be less than today\'s date. Please choose a date at least 48 hours from today\'s date.",nIcon:1});
else{
if (strB.valueOf() <= strA.valueOf())
// if ( event.fieldFull || event.willCommit )
this.getField("Spec1OLDstartTime").setFocus();
}

George Kaiser

hfelsh
Registered: Dec 21 2007
Posts: 9
Thanks for the reply; unfortunately, that code doesn't work either. [img]http://www.theamazingcams.com/animated_icons/darn.gif[/img] If today's date is 12/21/2007 (and that's what is in the "Today" field), and I enter 12/25/2007 (which is greater than today, obviously) in the Spec1OLD field, the error is displayed telling me that day/date cannot be less than today's date. Same if I enter a date before today (which is what I want, of course.) I also get the error if I blank out the text, which I don't want (blanking it out should = no validation, hence the comparison I was doing.) That part of the code actually seems to work from my original code... [img]http://tampabaymiatas.net/forum/images/smiles/eusa_think.gif[/img]

The "48 hour" text is only a recommendation, so it doesn't need to be calculated.

I'm open to any suggestions...this is killing me!

edit: BTW, thanks for the tip on the conosole in the code. That helps. This is what shows up when I enter 02/02/2008 in the Spec1OLD field:

Today:entry: Fri Dec 21 2007 13:56:36 GMT-0500 (Eastern Standard Time)length: undefinedvlaue: 1198263396315date:entry: Fri Dec 21 2007 12:00:00 GMT-0500 (Eastern Standard Time)length: undefinedvlaue: 1198256400315B > A: false

Firstly, what the heck is "vlaue"? [img]http://www.theamazingcams.com/animated_icons/thinking.gif[/img] I copied that exactly, shouldn't it be "value"? Secondly, it appears to be seeing only the "Today" field, not the "Spec1OLD" field, and that's why it's failing. Unless I'm reading it wrong...? [img]http://forum.miata.net/vb/images/smilies/confused.gif[/img]
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Carefully read the conosle output. The entry date for "strB" is less than "strA". It looks like somewhere else you are forcing the value of "Spec1OLD" to a different value. The entry is showing the date, time, UTC adjustment and the time zome for the today field and the "Spec1OLD" field.

The value of the date time object in JavaScript and Acrobt AroForms JavaScript is the milliseconds since midnight Jan 1, 1970.

George Kaiser

hfelsh
Registered: Dec 21 2007
Posts: 9
The field Spec1OLD doesn't appear anywhere else in the form (and I've checked for dupes), so that's odd... [img]http://www.theamazingcams.com/animated_icons/thinking.gif[/img] As far as I can tell, there's nothing else forcing Spec1OLD to anything different. I mean, I have the same code in the field Spec2OLD, but the variables are not the same letters. I removed the validation script from there just to test, same error and output.

I am really confused about this...it makes no sense! [img]http://tampabaymiatas.net/forum/images/smiles/eusa_wall.gif[/img]
hfelsh
Registered: Dec 21 2007
Posts: 9
gkaiseril wrote:
You can not compare the lengths of the date time object you need to compare the value of the date time objects using the "valueOf()" method to get the value.
I just wanted to point out that according to this article:

http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/date_time_part2/index.php

Is the code I used to compare the lengths:
if(strStart.length || strEnd.length){

(Changed for my variables, of course.)

Also, it looks like your example had the < symbol in both statements, but even after changing it to >= (greater than or equal), it still throws the error. [img]http://tampabaymiatas.net/forum/images/smiles/icon_cry.gif[/img]
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
You have taken one line from a piece of code and changed the context of the code. The code of the article:

var strStart = this.getField("DateStart").value;var strEnd = this.getField("DateEnd").value;if(strStart.length || strEnd.length){

You will note the variables hold the "value" of the referenced field objects and not the date time object of the fields' values.

The following code may more clearly disclose the difference between properties of a field's object value and the date time object derived from a field's value.

var strA = this.getField("Today").value; // get today's date valuevar strB = this.getField(event.target.name).value; // get the field value var objA = util.scand("mm/dd/yyyy", strA);var objB = util.scand("mm/dd/yyyy", strB); // some added debugging code for informationconsole.show()console.println("Today:")console.println("field value:" + this.getField("Today").value);console.println("this.getField(\"Today\").value.length: " + this.getField("Today").value.length);console.println("strA date time object: " + objA);console.println("strA date time object length: " + objA.length);console.println("strs data time object as a vlaue: " + objA.valueOf() );console.println("");console.println("Spec1OLD:")console.println("field value:" + this.getField("spec1OLD").value);console.println("this.getField(\"spec1OLD\").value.length: " + this.getField("spec1OLD").value.length);console.println("objB date time object: " + objB);console.println("objB date time object length: " + objB.length);console.println("objB data time object as a vlaue: " + objB.valueOf() );console.println("objB.valueOf() > objA.valueOf(): " + ( objB.valueOf() > objA.valueOf() ) );// end debugging code if (strA != "" & strB != "") {if ( objA.valueOf() < objB.valueOf() )app.alert({cMsg:"Day\/date cannot be less than today's date. Please choose a date at least 48 hours from today's date.",nIcon:1});else  this.getField(event.target.name).setFocus();}

George Kaiser

hfelsh
Registered: Dec 21 2007
Posts: 9
That code helps. I'm now seeing this at the bottom of the Debugger:
"this.getField ("spec1OLD") has no properties17:Field:Validate

When I remove the debugging code, I do not get the prompt though.