Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Validating date

by Anonymous Monk
on May 24, 2004 at 13:35 UTC ( #355900=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to validate date entry where it accepts only dates with two or four number in the year part and doesn accept anything else like five, or one or three numbers in the year part. This is the only thing I want if someone was entering a date for October 23 2004:
10/23/04 or 10/23/2004

The below isnt working.
/^\d{1,2}\/\d{1,2}\/\d{1,4}/

Replies are listed 'Best First'.
Re: Validating date
by Happy-the-monk (Abbot) on May 24, 2004 at 13:42 UTC

    The regex is missing a right anchor $ for one thing... have a look into   perldoc perlre   for alternates like   (?:\d\d|\d{4}).

    Also have a look at modules like   Time::ParseDate   and the like for actually valuating the numers for dates that make sense.

    Cheers, Sören

Re: Validating date
by dave_the_m (Prior) on May 24, 2004 at 13:41 UTC
    Replace the \d{1,4} with \d{2}(?:\d{2})?, ie two digits of the year followed by an optional further two digits

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://355900]
Approved by Happy-the-monk
help
Chatterbox?
Discipulus Eatalian is a sponge language..
[Eily]: Discipulus I like chiocciola :D

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2017-11-21 10:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (297 votes). Check out past polls.

    Notices?