Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Script to validate date fails

by Your Mother (Canon)
on Feb 16, 2012 at 15:46 UTC ( #954263=note: print w/ replies, xml ) Need Help??


in reply to Script to validate date fails

Adding to what runrig said, Date::Calc has the fastest date validation.

use strict; use warnings; use Date::Calc "check_date"; print "Enter the date in yyyy-mm-dd format : "; # More "native" format +. while ( my $date = <STDIN> ) { last if $date =~ /^$/; my @date = $date =~ /(\d+)/g; # Allows any separator and mushy inp +ut. if ( eval { check_date( @date ) } ) { print "You have entered valid data\n"; } else { print "You have entered invalid data\n"; } print "Enter the date in yyyy-mm-dd format : "; } __END__ Enter the date in yyyy-mm-dd format : ohai You have entered invalid data Enter the date in yyyy-mm-dd format : 2012-2-29 You have entered valid data Enter the date in yyyy-mm-dd format : 2011-2-29 You have entered invalid data Enter the date in yyyy-mm-dd format :


Comment on Re: Script to validate date fails
Download Code
Re^2: Script to validate date fails
by Not_a_Number (Parson) on Feb 16, 2012 at 17:15 UTC
    if ( eval { check_date( @date ) } )

    Why eval?

    if ( check_date( @date ) )

    works just as well.

    Update: Oops, no it doesn't! My version dies nastily if fed random alphanumerics or whatever.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://954263]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2014-12-19 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (91 votes), past polls