Welcome Guest, Not a member yet? Register   Sign In
Validate Drop Down Selections
#1

[eluser]Petsoukos[/eluser]
Hi,

I want to validate a date formed with drop down selections. I have three different drop down menus. (Year, Month, Day)
When it is submitted the controller receives three different POSTS. One for each drop down.

I can make the validation very easy with PHP checkdate() function:

Code:
$y = $this->input->post('y');
$m = $this->input->post('m');
$d = $this->input->post('d');
if(checkdate($m, $d, $y)) {
     return true;
} else {
     return false;
}
but if it fails how can I display the custom error message?
#2

[eluser]iConTM[/eluser]
I would make 1 field to input the date.

If you want to use CI's formvalidation class you can do something like this:

Code:
$this->form_validation->set_rules('date', 'Date', 'callback_date_check');

Code:
function date_check ($value)
{
    $parts = explode('/', $value);
    
    // make sure that all parts are digits. otherwise the checkdate method will throw errors
  
    if ( count($parts) != 3 || !checkdate($parts[1]  ,$parts[0]  ,$parts[2]) )
    {
        $this->load->library('validation');
        $this->validation->set_message(__FUNCTION__, 'The %s field must have dd/mm/yyyy format.');
        return FALSE;    
    }
        
    return true;
}
#3

[eluser]Petsoukos[/eluser]
That's the thing. I don't want to use a text input field. I would like to have the drop downs... every one is using drop downs.
#4

[eluser]iConTM[/eluser]
The date check finetuned:
Code:
function date_check ($value)
{
    //match the format of the date
    if ( preg_match ("#^([0-9]{2})/([0-9]{2})/([0-9]{4})$#", $value, $parts) )
    {            
        //check weather the date is valid of not
        if( checkdate($parts[2],$parts[1],$parts[3]) )
        {
            return true;
        }
    }
                
    $this->load->library('validation');
    $this->validation->set_message(__FUNCTION__, 'The %s field must have dd/mm/yyyy format.');
    return FALSE;    
}
#5

[eluser]iConTM[/eluser]
[quote author="Petsoukos" date="1283378428"]That's the thing. I don't want to use a text input field. I would like to have the drop downs... every one is using drop downs.[/quote]

I don't agree about that Wink

I notice that the dropdowns are more and more replaced by textfields together with a datepicker. is more userfriendly (only one click)

jquery datepicker: http://www.kelvinluck.com/assets/jquery/...r/v2/demo/
#6

[eluser]iConTM[/eluser]
or:

Code:
// run the validation on 1 of the dropdowns
$this->form_validation->set_rules('d', 'Date', 'callback_date_check');

Code:
function date_check ($value)
{
    $y = $this->input->post('y');
    $m = $this->input->post('m');
    $d = $this->input->post('d');

    if( checkdate($m,$d,$y) )
    {
        return true;
    }
                
    $this->load->library('validation');
    $this->validation->set_message(__FUNCTION__, 'The %s field is not a valid date.');
    return FALSE;    
}
#7

[eluser]Petsoukos[/eluser]
I tried the jQuery datepicker but for some odd reason when I click on the text field it just displays it in the main body of the page and not as a "pop-out" frame. Can't figure out how to make it work.
#8

[eluser]iConTM[/eluser]
You have to include the css:

http://www.kelvinluck.com/assets/jquery/...Picker.css
#9

[eluser]Petsoukos[/eluser]
I didn't forget...

Code:
<link rel="stylesheet" href="<?php echo base_url();?>application/views/templates/default/css/datePicker.css" type="text/css" media="screen" charset="utf-8" />
script type="text/javascript" src="<?php echo base_url();?>application/views/templates/default/js/jquery.1.4.2.js">[removed]
script type="text/javascript" src="<?php echo base_url();?>application/views/templates/default/js/date.js">[removed]
script type="text/javascript" src="<?php echo base_url();?>application/views/templates/default/js/jquery.datePicker.js">[removed]

This is the code from my header.

Maybe the CSS file has to be on a different location...
#10

[eluser]iConTM[/eluser]
Does the link to your css file work?
Try view source and copy paste the css link in your browser.




Theme © iAndrew 2016 - Forum software by © MyBB