07-25-2006, 07:28 AM
Wow, almost a year and half has gone by and I never posted my own solution to this challenge.
This is very old code that I've been using for years. Try it, you'll' like it.
This is very old code that I've been using for years. Try it, you'll' like it.
Code:
DEFINT A-Z
DECLARE FUNCTION NumStrict (Z$)
DECLARE FUNCTION IsLeapYear% (Z)
rem Setup days-per-month table.
DIM ZMO(1 TO 12)
DATA 31,28,31,30,31,30,31,31,30,31,30,31
FOR ZMM=1 TO 12:READ ZMO(ZMM):NEXT ZMM
REM ***********************************************************************
input "Enter date to be validated as YYYYMMDD ",z$
gosub date.check
if date.ok=0 then print "Invalid Date" else print "Date ok"
SYSTEM
REM **************** DATE.CHECK SUBROUTINE **************************
REM *
REM *** VALIDATE A DATE IN YYYYMMDD FORMAT.
REM *
REM * INPUT: Z$ = Given date in format YYYYMMDD.
REM *
REM * OUTPUT: DATE.OK = -1 if input date is VALID. (true)
REM * 0 if input date is INVALID. (false)
REM *
DATE.CHECK:
DATE.OK = 0 'preset to false
IF LEN(Z$)<>8 THEN RETURN
IF NOT NUMSTRICT(Z$) THEN RETURN
ZDD=VAL(RIGHT$(Z$,2)) 'Set day
ZMM=VAL(MID$(Z$,5,2)) 'Set month.
ZYY=VAL(LEFT$(Z$,4)) 'Set year.
IF ZMM<1 OR ZMM>12 OR ZDD<1 OR ZDD>31 THEN RETURN
IF ZMO(ZMM)+1*(-(ZMM=2 AND ISLEAPYEAR(ZYY))) < ZDD THEN RETURN
' If expression (month=2 and is leapyear) is TRUE which is -1, then
' taking the negative of this issues a plus 1. Conversely, FALSE
' always gives a zero. Multiplying the +1 by this result of 1 or 0
' will either add 1 or not to the number of days in the month.
' The logic wants to add 1 only when it is February and leap year.
DATE.OK = -1 '-1=valid (true)
RETURN
END
' ====================== ISLEAPYEAR ==========================
' Determines if a year is a leap year or not.
' ============================================================
'
FUNCTION IsLeapYear (Z) STATIC
' If the year is evenly divisible by 4 and not divisible
' by 100, or if the year is evenly divisible by 400, then
' it's a leap year:
IsLeapYear = (Z MOD 4 = 0 AND Z MOD 100 <> 0) OR (Z MOD 400 = 0)
END FUNCTION
' =================================================================
FUNCTION NumStrict (Z$)
'
' *** CHECK FOR STRICTLY NUMERIC (NO NULL, NO NEGATIVE, NO DECIMAL)
'
NumStrict=0 'Init to False
IF Z$="" THEN EXIT FUNCTION
FOR X = 1 TO LEN(Z$)
A=ASC(MID$(Z$,X,1))
IF A<48 OR A>57 THEN EXIT FUNCTION
NEXT X
NumStrict = -1 'True
END FUNCTION