$pattern='^(0[1-9]|[12][0-9]|3[01])[- /](0[1-9]|1[012])[- /]((19|20)\d\d)$';
#!C:/strawberry/perl/bin/perl.exe # path to my perl compiler, change it with yours ex: #!/usr/bin/perl
#########################################
# Author: Sanjeev Kumar Jaiswal
# Date: October 2010
# Purpose: Date Validation in any format you wish.
# ex: dd-mm-yyyy or dd/mm/yyyy
########################################
use strict;
use warnings;
my @today = localtime(); #get current system time
my $year = $today[5]+1900;
my $month = $today[4]+1;
my $date = $today[3];
print "Todays is ", scalar localtime, "\n";
print "This program is to check the validation of date\n From 1900 to till now i.e.", $today[5]+1900 ," only\n";
print "Please enter the date in specified format i.e. DD/MM/YYYY or DD-MM-YYYY\n";
my $mydate=<STDIN>; # to get the input from keyboard
chomp($mydate); # have to remove \n from the last that came after hitting enter
# Pattern for date validation
#dd-mm-yyyy from 1900-2099 (Year)
my $pattern='(0[1-9]|[12][0-9]|3[01])[- /](0[1-9]|1[012])[- /]((19|20)\d\d)';
# If it matches the pattern then
if($mydate=~ m/$pattern/){
if($2==02 && $1>29){
print "February can't be of more than 28 or 29 days";
exit;
}
if(!($3 % 4 == 0 && ($3 % 100 != 0 || $3 % 400 == 0)) && $2==2 && $1==29){
print "Its not a leap year, so please type the date between 1 and 28";
exit;
}
if($1 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)){
print "This month only allow upto 30 as th date input";
exit;
}
if($3>$year){
print "The provided date is the future date i guess. its year should be less or equal to $year\n";
exit;
}
if($3 >= $year){
if($2>$month){
print "You have entered $2 at Month's place which is ahead of $month";
exit;
}
else{
if($1>$date){
print "You have entered $1 at Date's place which is ahead of $date";
exit;
}
}
}
print "You have entered $mydate, which is correct according to us";
}
else{
print "$mydate does not follow the pattern DD/MM/YYYY or DD-MM-YYYY or \n you have entered some junk date like\n 1. 32 as date or \n 2. 13 as month or\n 3. 3100 as year";
}
############# End Program ################
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (5)
Commented:
Not that you *could* do it with regex, but I don't see where option "d" is covered by that pattern. The above statement is a bit misleading. Actually, the title of this article itself is a bit misleading. The title implies that regex itself can be used to validate dates. While I have seen very intricate patterns which did a fairly decent job, using regex to validate dates is a messy business. I think at best, you could use them to validate the proper structure of a date (e.g. DD/MM/YYYY vs DD-MM-YYY), but with regard to validating whether or not a date is valid, that should be left up to complete logic, as your post demonstrates.
Author
Commented:That is why i have used (0[1-9]|[12][0-9]|3[01]) for date dd patter instead of blindly using \d.
And it would reduce programmers time too.
Commented:
Author
Commented:I just tried to keep it as simple as i can. Otherwise validating i na single would make it more complex and less preferable.
Commented: