Custom form validation in codeigniter

Rakesh Sharma      2 Comments  

How to use custom form validation in codeigniter :-

codigniter have most of form validations inbuilt but sometime we need to apply our own or custom validation on form fields like mobile, email etc. In codeigniter we can easily apply our custom validation to form. there is a simple process to apply custom form validation in codeigniter we can apply custom validation using rules or using library. let’s see how to create custom validation in codeigniter there are two method for this :-

1. Extending form validation in codeigniter :-

Create a file with named “MY_Form_validation.php” in application/libraries/ and add below code.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
     * MY_Form_validation::alpha_extra().
     * Alpha-numeric with periods, underscores, spaces and dashes.
    function alpha_extra($str) {
        $this->CI->form_validation->set_message('alpha_extra', 'The %s may only contain alpha-numeric characters, spaces, periods, underscores & dashes.');
        return ( ! preg_match("/^([\.\s-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE;
    // add more function to apply custom rules.

Note :- function body must have a message $this->form_validation->set_message(‘rule’, ‘Error Message’); and return a Boolean value TRUE/FALSE.

Applying validation to field :- go to your controller function and call above created validation rule.

$this->form_validation->set_rules('my_field_name', 'My Field Label', 'trim|required|alpha_extra');

2. Using callbacks :- we can directly apply or create validation rules in controller using callback. callbacks use our custom validation functions and extends the validation class like we need to run a db query for validation etc.
let’s create an example which adding year check validation.
Add a function like below to your controller :-

function year_validation($str) {
  // $str will be field value which post. will get auto and pass to function.
  $year = date('Y');
  if ($str <= $year) {
    $this->form_validation->set_message("year_validation", 'year should be greater then current year.');
    return FALSE;
  else {
    return TRUE;

applying this rule to field :-

$this->form_validation->set_rules('year', 'Year', 'trim|required|xss_clean|callback_year_validation');

3. Editing system default validation :- this is not a good practice but if need go to “system/libraries/Form_validation.php” and edit validations rules and save. if you need to change also system default validation messages then go to “system/language/english/form_validation_lang.php” and edit vaidations messages.

So you are all set to make custom validation in codeigniter. For more about validation follow CI Form Validation.

  • abhi

    I like this post, its really helpful but I have query who to set custom validation message for specific field.

    • rakesh sharma

      You can pass custom label on setting up rules like

      $this->form_validation->set_rules(‘field_name’, ‘YOUR_LABEL’, ‘rules’);