Re: Help with validating a date input to a dialog
Re: Help with validating a date input to a dialog
- Subject: Re: Help with validating a date input to a dialog
- From: Paul Berkowitz <email@hidden>
- Date: Fri, 30 Mar 2001 18:46:22 -0800
On 3/30/01 6:04 PM, "Rob Jorgensen" <email@hidden> wrote:
display dialog (my setthedate("Please enter the date in format
"mm/dd/yyyy"."))
on setthedate(prompt)
set DateToSend to (text returned of (display dialog prompt default
answer ""))
try
date DateToSend
on error
my setthedate("Please us the correct format "mm/dd/yyyy"")
end try
return DateToSend
end setthedate
>
>> This script performs as expected on my system (OS 9.1/AppleScript
>
>> 1.6/No Language kits installed/No OS X). I tried it in Script Editor,
>
>> Script Debugger and as an application.
>
>
>
> When you enter something with a wrong format, then enter again with a
>
> correct format, it returns the second entry?
>
>
No it doesn't, it returns the first entry. I apologize for not
>
testing thoroughly.
Assuming you've put the escape characters back in, add a
global DateToSend
either at the top of the script or the top of the handler, it doesn't
matter. What's happening is that after running the handler recursively (and
returning the right date) in the 'on error' section, it proceeds to the
final line of the handler 'return dateToSend' in the original iteration of
the handler. Since datToSend is a local variable when you haven't declared
it global, its value _here_ is the original date format.
Another few things to remember:
1) On a user's machine which has a different format in Date & Time control
panel (precisely the sort of user who will want to enter in a different
format such as dd/mm/yyyy), the script will do exactly the opposite of what
you want: it will error when the format is different from the user's.
2) If the month and day are inverted but the day is 12 or less, it won't
error, but it won't be what you want
3) If the user puts in a two-digit year, AppleScript won't find the date
invalid even if "month" is greater than 12, as long as it's less than 24 and
the year less than 60, since AppleScript will then assume it's actually the
time (including seconds. like "23:07:01"), in 24-hr clock, of the date you
compiled the script, and will not do the second run through the handler.
--
Paul Berkowitz