in reply to Simple Date Validation

Looking at the Time::Local documentation, I see the following:

The timelocal() and timegm() functions perform range checking on the input $sec, $min, $hour, $mday, and $mon values by default.

So, I would just exploit that and hand the parts of the date to, say, timelocal, and see if it accepts these values as valid or not:

my ($sec,$min,$hour) = qw( 0 0 12 ); my ($mday, $mon, $year) = qw(11 5 2007); $mon--; # because that's how unix/C treat the month value eval { my $dummy = timelocal($sec,$min,$hour,$mday,$mon,$year); }; if (my $err = $@) { print "This is an invalid date."; } else { print "Yay"; };

Splitting up $userdate into the parts making up the day, month and year is left as an exercise to the reader.

Replies are listed 'Best First'.
Re^2: Simple Date Validation
by Trihedralguy (Pilgrim) on May 11, 2007 at 15:38 UTC
    How do I go about handing a date that is in this format: MM/DD/YYYY into that type of validation?
      $userdate = "12/31/2005"; my ($mon, $mday, $year) = split /\//, $userdate;

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Just split it.

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re^2: Simple Date Validation
by ad1mt (Initiate) on Sep 25, 2008 at 15:15 UTC
    unfortunately timelocal does not validate dates properly...

    # perl -e 'use Time::Local; print timelocal(0,0,0,31,02,2007);'
    1175295600
    #

      Works as designed and documented. Maybe you didn't mean "02" as the month index but "01" (for February)?

      Q:\>perl -MTime::Local -e "print timelocal(0,0,0,31,01,2007)" Day '31' out of range 1..28 at -e line 1