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: email@hidden
- Date: Sat, 31 Mar 2001 22:14:02 -0500
On Fri, 30 Mar 2001 20:28:39 -0800, Paul Berkowitz <email@hidden> wrote,
>
On 3/30/01 7:29 PM, "Michelle Steiner" <email@hidden> wrote:
>
> On 3/30/01 6:46 PM, Paul Berkowitz <email@hidden> wrote:
>
> >
>
>> Assuming you've put the escape characters back in, add a
>
>>
>
>> global DateToSend
>
>
>
> that did it; thanks.
>
>
There are some people who don't like globals (seems unfair to me), although
>
I haven't seen Scott Norton around in a while!
Be careful of whose name you mention. Like the sailors say about the Evil One,
"Naming calls." Since I've been doing web development on the Dark Side for the
last two months, I'm feeling quite satanic. >:-[>
>
Still, before anyone jumps on
>
me, there's another way to do this that doesn't require a global: just set
>
DateToSend in the 'on error' statement to the recursion which returns (its
>
own) DateToSend. Like so:
I wouldn't do this recursively. Its not really a recursive problem. Sure, the
code will run, and any limits on recursion won't hurt, because no user is so
stupid and so persistent to be able to overrun the stack by repeatedly making an
error, but the problem is basically iterative, and calls for a repeat loop.
Writing it otherwise is misleading to anyone else reading the code (which would
include the original writer, six months later.)
Here's my try:
display dialog "Enter a date" default answer ""
set possibleDateToSend to text returned of result
repeat
try
set DateToSend to (date possibleDateToSend)
exit repeat
end try
display dialog "Enter a date, and do it my way!" default answer ""
set possibleDateToSend to text returned of result
end repeat
You could move the first "set possibleDateToSend..." after the "repeat" and
eliminate the second "set possibleDateToSend..." and get the same result, but I
think that would make it harder to read. The reader would have to realize that
"result" is carried across the loop iteration. I don't mind using "result", but
always use it in a block of statements I write as a "paragraph"--that is, a
series of statements that I clump together and separate from the rest of the
script with blank lines. Carrying "result" around from the back of the loop to
the front goes against this rule of style.
With regard to date format, the original request says, 'Please enter the date in
format \"mm/dd/yyyy\".' In fact, the coercion permits a wider range of formats
(like "1 Jan", which coerces today to "Monday, January 1, 2001 12:00:00 AM").
But the coercion also depends on the Date & Time settings to determine whether
"03/02/01" means March second or February third.
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden