Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write a bulletproof date validation routine.
#51
Moneo: The date routine has one minor flaw, that other posters in the thread checked for.

I would check for Len(Z$) <> 8 first, because this would indicate a bad date.

One entry that passes your check is 200002228 even though it is 9 chars.

Otherwise, great job.

I would probably do this for the day and month check, it appears more logical to me, it took me a while to figure yours out.

Code:
IF ZMM < 1 OR ZMM > 12 THEN RETURN
IF ZDD < 1 OR ZDD > (ZMO(ZMM) - ((ZMM = 2) AND ISLEAPYEAR(ZYY))) THEN RETURN
EVEN MEN OF STEEL RUST.
[Image: chav.gif]
Reply
#52
Quote:Moneo: The date routine has one minor flaw, that other posters in the thread checked for.

I would check for Len(Z$) <> 8 first, because this would indicate a bad date.

One entry that passes your check is 200002228 even though it is 9 chars.

Otherwise, great job.

I would probably do this for the day and month check, it appears more logical to me, it took me a while to figure yours out.

Code:
IF ZMM < 1 OR ZMM > 12 THEN RETURN
IF ZDD < 1 OR ZDD > (ZMO(ZMM) - ((ZMM = 2) AND ISLEAPYEAR(ZYY))) THEN RETURN
You're very observant, Yetifoot.

I fixed the code above to add the check for len(Z$)<>8.
Sorry, the program of mine, that I lifted this code from, had already checked this, and my poor testing of this new program didn't reveal the error. Thanks.

Yeah, the code regarding February and leapyear is too hairy. That's why it needed 5 lines of comments to explain. You're right. It's unnecessarily complex. I thought it was cute at the time, back in 1994, when I was enamored with bitwise logic. I won't bother to change it now, because it does work. But feel free to make a change to simplify it.

Regards and thanks..... Moneo
Reply
#53
I prefered your method of hard coding the number of days per month, whilst i enjoyed the other posts methods of determing the number of days in a month (complicated, but interesting), I agree with the idea of keeping it fairly understandable when possible.

I like the IsLeapYear function also, I guess at first i would have been tempted to write a multiline IF..THEN based code, but it really is not necessary as shown in yours. Earlier in the thread you said you felt your IsLeapYear was overcomplicated, but i disagree, i felt it was still very understandable.
EVEN MEN OF STEEL RUST.
[Image: chav.gif]
Reply
#54
Quote:I prefered your method of hard coding the number of days per month, whilst i enjoyed the other posts methods of determing the number of days in a month (complicated, but interesting), I agree with the idea of keeping it fairly understandable when possible.

I like the IsLeapYear function also, I guess at first i would have been tempted to write a multiline IF..THEN based code, but it really is not necessary as shown in yours. Earlier in the thread you said you felt your IsLeapYear was overcomplicated, but i disagree, i felt it was still very understandable.
Yeti, thanks for you constructive comments. I think we are now pretty much in agreement on all the coding issues of this task.

I hope we have impressed on others the need of having such a date validation routine tested and ready to be used in any program which receives a date as input from the user or even as input from another file.
Most programs don't contain good date validation logic simply because the programmer doesn't want to take the time to do it and test it while writing the main program. Often, the same happen with other validations like numeric amounts, numerics with decimals, validating codes, zipcodes, phone numbers, name and address fields, etc. These can be a pain in the butt if you don't have the logic ready-made, so they usually go unchecked --- "garbage in, garbage out."

Regards..... Moneo
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)