4

I have a past date for a textbox, and when I want to validate the form submit if the date is greater than the current date and has the format dd/mm/yyyy.

in my form_validation.php:

'add_prod_serv_fact' => array(
    'quantidade_prod'           => array('field' => 'quantidade_prod',          'label' => 'Quantidade',        'rules' => 'required|trim|numeric|greater_than[0]|htmlspecialchars'),
    'desconto_prod'             => array('field' => 'desconto_prod',            'label' => 'Desconto',          'rules' => 'required|trim|numeric|greater_than[-1]|less_than[101]|htmlspecialchars'),
    'date'                      => array('field' => 'date',                     'label' => 'Date',              'rules' => 'required|trim|htmlspecialchars')
)

How do I validate the date?

Prix
  • 19,417
  • 15
  • 73
  • 132
pc_oc
  • 535
  • 3
  • 8
  • 26
  • 1
    You can add custom validation functions in CI: http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks – sinisake Jul 09 '13 at 01:36
  • yes yes, but what use regex? thanks! – pc_oc Jul 09 '13 at 01:39
  • possible duplicate of [Codeigniter - Date format - Form Validation](http://stackoverflow.com/questions/14359158/codeigniter-date-format-form-validation) – Prix Jul 09 '13 at 02:22
  • 2
    I wouldn't use regex, i would use simpler solutions: http://stackoverflow.com/questions/113829/how-to-convert-date-to-timestamp-in-php. You will get some ideas from here, i am sure... DateTime functions are great. :) – sinisake Jul 09 '13 at 02:34

3 Answers3

6

You can validate through regular expression first you should write a function like this

function validate_date_reg($input)
{
   if (preg_match('\d{1,2}/\d{1,2}/\d{4}', $input))
   {
      return true; // it matched, return true
   }
   else
   {
      return false;
   }
}

and call it like this:

if($this->validate_date_reg($this->input->post('some_data')))
{
    // true go ahead......
}

i hope that this will help you.

MJ X
  • 8,506
  • 12
  • 74
  • 99
3

1) How to validate a date with CI callbacks:

"The validation system supports callbacks to your own validation functions. This permits you to extend the validation class to meet your needs."

In your method:

$this->form_validation->set_rules('text_date', 'Date', 'trim|exact_length[10]|callback_validate_date');

Your own callback function:

public function validate_date($incoming_date)
{
    //If in dd/mm/yyyy format
    if (preg_match("^\d{2}/\d{2}/\d{4}^", $incoming_date))
    {
        //Extract date into array
        $date_array = explode('/', $incoming_date);

        //If it is not a date
        if(! checkdate($date_array[1], $date_array[0], $date_array[2]))
        {
            $this->form_validation->set_message('validate_date', 'Invalid date');
            return false;
        }
    }
    //If not in dd/mm/yyyy format
    else
    {
        $this->form_validation->set_message('validate_date', 'Invalid date');
        return false;
    }

    return true;
}

2) How to compare two dates:

$date_one = (int) strtotime(str_replace('/', '-', $this->input->post('date_one', true)));
$date_two = (int) strtotime(str_replace('/', '-', $this->input->post('date_two', true)));

if($date_one < $date_two)
{
    echo 'Message';
}
else
{
    echo 'Message';
}
BentCoder
  • 12,257
  • 22
  • 93
  • 165
2

try out this..

you need to down date.js from this url "https://code.google.com/p/datejs/downloads/list"

call datefunction() function form onChange()

<script>
    function datefunction()
    {
        var startdate = document.getElementById('date1').value;
        var enddate  = document.getElementById('date2').value;
        // for current date use
        // var enddate  = new Date();

        var d1 = Date.parse(startdate );
        var d2 = Date.parse(enddate  ) ;

        if (d1 > d2) 
        {
            alert ("Start Date cannot be gratter than End Date!");

            return false;
        }
    }
</script>
Vijay
  • 8,131
  • 11
  • 43
  • 69