This may be too much but validation of date info to me is necessary.
Otherwise junk in , junk out.  All too often people put 9/31/01 which is
invalid. So why not use the system to make sure. SO just using timelocal and
localtime, you can make sure that the date entered is valid.

#!perl -w

use Time::Local;

my @DateUse = ();
while ( 1 ) {
   printf "Please enter date to check: ";
   chomp(my $Dates = <STDIN>);
   last if ($Dates =~ /^ex/i );
   
   if ( length($Dates) < 6 || $Dates !~
/^(\d{1,2})[^0-9]{0,1}(\d{1,2})[^0-9]{0,1}(\d{1,4})/ ) {
      printf "Expecting some form of a date in format:\n";
      printf "mm/dd/yy, mmddyy, m/d/yy, m/d/yyyy\n";
      printf "You entered: <$Dates>\n";
      printf "Please enter correct data\n";
      next;
    }
   undef @DateUse;
   push(@DateUse,($1,$2,$3));
   printf "%d\n", scalar(@DateUse);
   
   if ( ! (scalar(@DateUse) and ! (scalar(@DateUse) % 3) ) ) {
      die "Expecting groups of 3 for date calculations:$!";
    }
    
   for(my $MyId=0;$MyId<scalar(@DateUse);$MyId+=3) {
      my $t1 =
timelocal(0,0,12,$DateUse[$MyId+1],$DateUse[$MyId]-1,$DateUse[$MyId+2]);
      printf "t1: %9d\n", $t1,;
      my $MyTime1 = localtime($t1);
      printf "LocalTime1: %-s\n", $MyTime1;
      my @MyTimeInfo = localtime($t1);
      if ( length($DateUse[2]) > 3 ) {
         $MyTimeInfo[5]+=1900;
       }elsif ( length($DateUse[2]) < 3 ) {
         $MyTimeInfo[5] = $MyTimeInfo[5] % 100;
       }
      # MyTimeInfo [0] = Seconds
      #            [1] = Minutes
      #            [2] = Hours
      #            [3] = Day of month
      #            [4] = Month minus 1
      #            [5] = Year (from 1900)
      #            [6] = Day of Week(Mon=1 thru Sun=7);
      #            [7] = Day of year
      #            [8] = Day light Savings(0: off 1: On)
      #
      if ( $DateUse[0] != $MyTimeInfo[4]+1 or $DateUse[1] != $MyTimeInfo[3]
or
           $DateUse[2] != $MyTimeInfo[5] ) {
         printf "Did not come back with the same date inputted:\n";
         printf "Month: I/P: %2d  Gen: %2d\n", $DateUse[0],
$MyTimeInfo[4]+1;
         printf "Day  : I/P: %2d  Gen: %2d\n", $DateUse[1], $MyTimeInfo[3];
         printf "Year : I/P: %2d  Gen: %2d\n", $DateUse[2], $MyTimeInfo[5];
       }    
   }
 }
 

-----Original Message-----
From: Dan Grossman [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, July 25, 2001 16:36
To: [EMAIL PROTECTED]
Subject: Re: Date verification


Terribly sorry if this gets posted twice ...

On Tue, 24 Jul 2001, Mark Byerley wrote:
> Good Morning/Afternoon to all
> 
> I need some error checking (or rather field validation) within my script
> itself to ensure a date field coming across on a form is in the format of
> dd-mm-yyyy. I would rather not have to strip the field down to check the
> individual numbers and then put them back together again. Is there any
> "simple" type of error checking I can use in this instance? I appreciate
the
> help.
> Thank you
> M~

How about pattern matching?

if ($date !~ m/\b(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[0-2])-\d{4}\b/) {
        reject ...
} else {
        accept ...
}

Note that the day and month matchings are robust within themselves
(i.e. no day "00" or month "19") but are not robust against each other
(i.e. "31-09-xxxx" would be accepted).  Also my year check is not
robust at all. :)

Daniel


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to