Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write a bulletproof date validation routine.
#46
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.
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
Reply


Messages In This Thread
My entry - by Meg - 10-11-2004, 09:03 PM
updated - by Meg - 10-12-2004, 08:01 PM
/nod - by Meg - 10-23-2004, 01:34 AM
Foo - by ToohTooh - 10-26-2004, 10:36 PM
Reply on Clarification Suggestion - by Neo - 10-26-2004, 11:37 PM
Re: Reply on Clarification Suggestion - by oracle - 10-27-2004, 02:00 AM
Hmm... - by ToohTooh - 10-27-2004, 02:12 PM
Re: Hmm... - by Neo - 10-27-2004, 10:12 PM
Thank you. - by ToohTooh - 11-01-2004, 04:54 PM
Write a bulletproof date validation routine. - by Moneo - 07-25-2006, 07:28 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)