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;

            }
    }
}

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.