CodeIgniter Forums
Date validation? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Date validation? (/showthread.php?tid=3196)



Date validation? - El Forum - 09-15-2007

[eluser]Mart[/eluser]
Hi

I was expecting to be able to use the CI Validation class to to validate dates on my forms. The user guide doesn't show any examples.

Does the validation class class support date checking?

How would I go about it otherwise? (Callbacks? I could use one of those I guess)

Cheers


Date validation? - El Forum - 09-15-2007

[eluser]Kemik[/eluser]
Yeah, I use a regex in a callback.


Date validation? - El Forum - 09-15-2007

[eluser]alpar[/eluser]
why regex? How about php's checkdate? I think it provides stronger validation than what you can achieve with a regex


Date validation? - El Forum - 09-15-2007

[eluser]Kemik[/eluser]
[quote author="alpar" date="1189898488"]why regex? How about php's checkdate? I think it provides stronger validation than what you can achieve with a regex[/quote]

I only use regex because my date is typed in to a text field so it's not in three variables.


Date validation? - El Forum - 09-15-2007

[eluser]Michael Wales[/eluser]
Personally, I like to incorporate a JS calendar for all of my date fields - Yahoo's UI is a great option.


Date validation? - El Forum - 09-16-2007

[eluser]Mart[/eluser]
I agree a regex would suit me best. Here's my callback

Code:
function date_check($date)
{
   $ddmmyyy='(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9]{2}';
   if(preg_match("$ddmmyyy", $date)) {
     return TRUE;
   } else {
     $this->validation->set_message('date_check', 'Please enter dd/mm/yyyy');
     return FALSE;
   }
}

I know the regex string is valid but I'm getting:

Code:
Message: preg_match() [function.preg-match]: Unknown modifier '['

Do I need to reformat my regex to be used in PHP?

Cheers


Date validation? - El Forum - 09-16-2007

[eluser]feri_soft[/eluser]
Why are you quoting the variable? I doubt thats the problem but however why is that:

Code:
if(preg_match("$ddmmyyy", $date)) {

and not

Code:
if(preg_match($ddmmyyy, $date)) {



Date validation? - El Forum - 09-16-2007

[eluser]Mart[/eluser]
Smile No reason other than me trying to work out why PHP doesn't like the regex (even though I know it works in QuickREx (Eclipse plugin for building & testing regex). I've tried both, btw...


Date validation? - El Forum - 09-16-2007

[eluser]Mart[/eluser]
Ha! - nailed it!

Code:
function date_check($date) {
   $ddmmyyy='(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)[0-9]{2}';
   if(preg_match("/$ddmmyyy$/", $date)) {
      return TRUE;
   } else {
     $this->validation->set_message('date_check', 'Please enter dd/mm/yyyy');
     return FALSE;
   }
}

It seems I needed to add "/" and "$/" to the front and back of the expression and then escape the x2 "/." with "\/." :cheese:

Told you I needed those double quotes ;-) (well not really I suppose)

Thanks for the input. And here's a working solution for others hopefully...


Date validation? - El Forum - 09-21-2007

[eluser]xcristi[/eluser]
Here is my validate function from libraries/Validation.php
Code:
function validdate($str)
    {
         if ( ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $str) ) {
            $arr = split("-",$str);     // splitting the array
            $yy = $arr[0];            // first element of the array is year
            $mm = $arr[1];            // second element is month
            $dd = $arr[2];            // third element is days
            return ( checkdate($mm, $dd, $yy) );
         } else {
            return FALSE;
         }
    }

The main improvement here is checkdate function.