CodeIgniter Forums

Full Version: Date validation?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4

El Forum

[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

El Forum

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

El Forum

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

El Forum

[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.

El Forum

[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.

El Forum

[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

El Forum

[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)) {

El Forum

[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...

El Forum

[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...

El Forum

[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.
Pages: 1 2 3 4