<?xml version="1.0" encoding="windows-1252"?>
<node id="954264" title="Re: Script to validate date fails" created="2012-02-16 10:49:44" updated="2012-02-16 10:49:44">
<type id="11">
note</type>
<author id="145694">
brx</author>
<data>
<field name="doctext">
&lt;code&gt;$date =~ /[1-31]-[1-12]-[1-99]/;&lt;/code&gt; doesn't do what you want.

&lt;p&gt;
&lt;code&gt;[...]&lt;/code&gt; is used for class of characters : you must list characters one-by-one "abcd" or a range like "a-k" or "0-6" (see [http://perldoc.perl.org/perlrecharclass.html] =&gt; 1) Bracketed Character Classes -- 2) Character Ranges )
&lt;/p&gt;
&lt;p&gt;
Here is a "DIY" validation :&lt;/p&gt;
&lt;code&gt;$date =~ /(0?[1-9]|[12][0-9]|3[01])-(0?[1-9]|1[012])-(\d?\d)$/&lt;/code&gt;
&lt;p&gt;
&lt;code&gt;(0?[1-9]|[12][0-9]|3[01])&lt;/code&gt; means...
&lt;/p&gt;
&lt;pre&gt;
accepts :
1 to 9 with an optional 0 prefix =&gt; 01,02,03...09 and 1,2,3...9
*OR*
(1 or 2) before (0 to 9) =&gt; 10,11,...,29
*OR*
30 or 31
&lt;/pre&gt;
&lt;p&gt;
...&lt;b&gt;but&lt;/b&gt; it's not the good way to do it (what about 29-02-01 for example?)
It's better to use a module which knows the calendar.
&lt;/p&gt;
&lt;p&gt;
Last thing : year with 2 digits ? not good ! :)
&lt;/p&gt;
&lt;p&gt;
My first try :
&lt;/p&gt;
&lt;code&gt;
#!/usr/bin/perl
use DateTime;

print ("Enter the date in dd-mm-yyyy format : ") ;
$date = &lt;STDIN&gt; ;
chomp ($date) ;

while ($date ne "") {
	my ($d,$m,$y) = split '-',$date;
	warn "/!\\ wrong year format? $y\n" if $y!~/^\d{4}$/ ;
        if ( eval {DateTime-&gt;new(year=&gt; $y,month=&gt; $m,  day=&gt; $d)}	) {
            print("You have entered valid data \n") ;
        } else {
            print ("You have entered invalid data \n") ;
        }
        $date = &lt;STDIN&gt; ; chomp($date) ;
}
&lt;/code&gt;



</field>
<field name="root_node">
954253</field>
<field name="parent_node">
954253</field>
</data>
</node>
