Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Script to validate date fails

by Your Mother (Chancellor)
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
Replies are listed 'Best First'.
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: (18)
As of 2015-07-07 17:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls