Some code I tend to use.

Problem I see with users providing dates: No way to make sure how they write it into the input field. It might be valid even if it's not the required format.
It's even possible a script might be used in Europe, where time format is different to US :)

examples of valid dates as of real life as I see them:
1.12.2006
01.12.2006
01.12.06
12/1/2006
12/01/06

right sub parse_datestring { # parses a datestring # - allows to provide a format of how to interprete day,month,year + in the datestring. # default format is: DD/MM/YY # -> calling this method with format=>'d.m.y' computes fa +ster than format=>'dd.mm.yy' ! # - any non-digit character in the datestring is considered a deli +miter! # - 2-digit years will be considered 1930-2029 # - by default the date calculated from the datestring will be val +idated # -> validate=>'0' disables validation # - return value is an array of ($day, $month, $year) # -> $day and $month are returned 2-digit # -> $year is returned 4 digit # ===> in case of invalid date ALL of the above will be unde +f # ===> in case of omitted day or month, the appropriate arra +y elements will be undef # - in case of errors such as invalid date $errstr is set and migh +t be retrieved by $handle->errstr(); # - example call: # my ($day,$mon,$year) = $handle->parse_datestring( 'datestring +' => '02.02.02', # 'format' => + 'd.m.y', # optional # 'validate' +=> 0, # optional # ) # my $self = shift; my %classargs = (@_); # reset $errstr $errstr = ''; my ($d,$m,$y); # make sure 'datestring' is not ommited! if (! $classargs{'datestring'}) { $errstr = 'ERROR parsing datestring: \'datestring\' is missing +'; } # make sure delimiter becomes '/' $classargs{'datestring'} =~ s/\D+/\//gi; # handle input like '06.2004' or '6.06' - assuming month of year if ($classargs{'datestring'} =~ /^(\d{1,2})\/(\d{2,4})$/gi ) { $m = $1; $y = $2; } # handle input like '2004' - assuming year elsif ($classargs{'datestring'} =~ /^\d{2,4}$/gi ) { $y = $classargs{'datestring'}; } # handle all other input - requesting days (possibly regardless of + the year like '06.08' # need to reorder according to format and delimiter else { if (! $classargs{'format'}) { # assume default format DD/MM/Y +Y(YY) ($d, $m, $y) = split(/\//, $classargs{'datestring'}); } else { # parse with provided formatting information my (%lookup,$first,$second,$third); #%position); # make sure delimiter becomes '/' $classargs{'format'} =~ s/[^dmyDMY]+/\//gi;right # need lower case $classargs{'format'} = lc ($classargs{'format'}); if (length($classargs{'format'}) >5) { $classargs{'format'} =~ s/([dmyDMY])+/$1/g; } ($first,$second,$third) = split(/\//, $classargs{'format'} +); ($lookup{$first}, $lookup{$second}, $lookup{$third}) = split(/\//, $classargs{'datestring' +}); $d = $lookup{'d'}; $m = $lookup{'m'}; $y = $lookup{'y'}; } } if (length($d) == 1) { $d = '0'.$d; } if (length($m) == 1) { $m = '0'.$m; } if (length($y) == 2) { if ($y >= 30) { $y += 1900; } else { $y += 2000; } } # validate date # NOTE: validate only if $m or $d are available! unless ($classargs{'validate'} eq '0') { my $m_index = int($m)-1; # validate month if ($m) { if (($m_index <0) || ($m_index >11)) { $errstr = "ERROR parsing datestring: not a valid month +"; } } if ($d) { # validate day my @days_per_month = (31,28,31,30,31,30,31,31,30,31,30,31) +; # check for leap-year if ( ((($y % 4) == 0) && (($y % 100) != 0)) || (($y % 400) == 0) ) { $days_per_month[1] ++; } # check day my $d_integer = int($d); if ( ($d_integer < 1) || ($d_integer > $days_per_month[$m_ +index]) ) { $errstr = "ERROR parsing datestring: not a valid day i +n month $m"; } } } return ($d, $m, $y); }

If others see assumtions being wrong or any other chance for improvement I'll appreciate that very much as well.

Hope it helps to find a solution to your problem.
RL

update
s/right/write/


In reply to Re: Simple Date Validation by RL
in thread Simple Date Validation by Trihedralguy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.