Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

What does validate mean in this context? Do you want to check whether the submitted date conforms to some format? Or do you want to check whether the submitted date is in some valid range?

It all depends on the format used in $userdate. Is it YYYY-MM-DD ? DD.MM.YY ? MM-DD-YYYY ? Jan 11 05 ? Does it contain time?

Without some restrition on the accepted format, there's no way to tell whether 01.02.03 is valid, and which part of that string means year, month and day respectively.

If you have some pre-defined format, a regular expression would suffice to check whether $userdate complies. Then you know which fields mean what. To convert e.g. Jan 12 2007 to a unix timestamp with Time::Local and validate there's no day or month overflow (e.g. Apr 31 2007):

use Time::Local; my $userdate = "Jan 12 2007"; my @months = qw (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov + Dec); @monthname {@months} = 0 .. 11; my ($m, $day, $year) = split /\s+/, $userdate; defined $monthname {$m} or die "no valid month\n"; my $month = $monthname {$m}; for ($day, $year) { /^\d+$/ or die "$_ invalid\n" } my $timestamp = eval { local $SIG{__DIE__}; timelocal (0, 0, 0, $day, +$month, $year) }; if ($@) { warn "invalid date: $@\n" } else { my @ltime = localtime ($timestamp); printf "date: %02d %s %04d\n", $ltime [3], $months [$ltime[4]], $l +time [5] + 1900; } print "done\n"; __END__

That properly warns of e.g. "Feb 31 2007" being an invalid date, but reports "29 Feb 2004" as being correct.

--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}

In reply to Re: Simple Date Validation by shmem
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found