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

Validate Date Entry for Wednesday

scshaw
Registered: Apr 18 2009
Posts: 12
Answered

I have read through Thom Parker's excellent 3-part series on dates in Javascript but am struggling with a validation of a date entry.
 
Need to check user input to see if date entered is a Wednesday and if it is not to generate an alert message to re-enter a correct Wednesday date (date format for field is yyyy/mm/dd).
 

My Product Information:
Acrobat Pro 9.3, Macintosh
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Accepted Answer
Convert the date string to a date and use the getDay method to get the day, which will be a number from 0 to 6. So if it returns 3, the date is a Wednesday.
scshaw
Registered: Apr 18 2009
Posts: 12
George,

Thank you. Just needed a push in the right direction.

Here is the resulting Custom Calculation Script for the field named AdEndDate2 (shown for only one of the two dates that must be a Wednesday)
__________________________
var dtEnd = new Date(this.getField("AdEndDate2").value);
var nDayEnd = dtEnd.getDay()

if( nDayEnd != 3)
{
app.alert("All dates must be a Wednesday.",2);
}
__________________________

Only a related question now: once the error is created and the alert message displayed and dismissed, the focus moves to the next tabbed field. How do you maintain focus on the field that created the error?

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
You should not use a calculation script for this, but rather a validate script. To get the focus to stay on the field, you can use the setFocus field method. The script could look like this:

// Custom Validate script
(function () {

// Create a date object.
var dEnd = util.printd("mm/dd/yyyy", event.value);

// Alert if not Wednesday
if( dEnd.getDay() !== 3) {

app.alert("All dates must be a Wednesday.", 2);

// Keep the focus in this field
event.target.setFocus();
}

})();


Note that the focus won't get set back to the field if the user commits the value by pressing the Enter key, but will if they click outside of the field with the mouse or attempts to tab to the next field. Replace the format string in the util.printd line to match the format you're using.



scshaw
Registered: Apr 18 2009
Posts: 12
George,

That sounds better. Will give it a go. I appreciate your input in solving this.

sc