0

Hi
I have added a custom form validation in codeigniter.
The callback function returns true/false & the value of validating field.

// JavaScript Document
class Test extends CI_Controller 
{

/*custom validation function*/
  function _checkdate()
  { 
      $post     =   $this->session->userdata('post');
      echo $fdate   =   $post['frmDt'];
      if (preg_match('/^[0-9-: ]*$/', $fdate)) 
      {
          return true;
      }
      else
      {
          return false;

      }

    }

    function submitform()
    {
            if($this->form_validation->run() == FALSE)
            { 
                        echo "Validation Failed";
                        exit;

            }
    }
}
2
Contributors
1
Reply
11
Views
3 Years
Discussion Span
Last Post by cereal
0

You have to set the validation rule in the submitform() method:

$this->load->library('form_validation');

$this->form_validation->set_rules('frmDt', 'Date', 'callback__checkdate');

Then you have to add an argument to the _checkdate() method because set_rules() will submit the value of the field specified in his first argument:

function _checkdate($date)
{
    $format = 'Y-m-d H:i:s'; # Must match the desired format
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) == $date;
}

Reference:

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.