In this tutorial we are going to learn about CodeIgniter Form Validation. Validation is an important process while building web application. It ensures that the data that we are getting is proper and valid to store or process. CodeIgniter has made this task very easy. Let us understand this process with a simple example.

Creates “myform.php” view file and saves the code below in “application/views/myform.php”. This page will display a form where users can submit their name and we will validate this page to ensure that it is not empty during submission.

<!DOCTYPE html>
<html>
<head>
  <title>TutorialPace.com | My Form</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

</head>
<body class="container">
  <div class="row mt-3">
    <form action = "" method = "">
       <?php echo validation_errors(); ?>  
       <?php echo form_open('form'); ?>  
       <h5>Name</h5> 
       <div class="form-group">
          <input type = "text" name = "name" value = "" size = "50" />
        </div>
        <div class="form-group">
          <input type = "submit" value = "Submit" />
        </div>    
    </form>  
  </div>
</body>
</html>

Creates and saves a view file in “application/views/formsuccess.php”. This page will be displayed if the form is successfully validated.

<!DOCTYPE html>
<html>
<head>
  <title>TutorialPace.com | My Form</title>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body class="container">
    <h3 class="h3">Your form was successfully submitted!</h3>  
    <p><?php echo anchor('form', 'Try it again!..', array('class' => 'btn btn-info')); ?></p>
</body>
</html>

Create and save a “Form.php” controller file to “application/controller/Form.php”.

<?php
   /* Form Controller */
   class Form extends CI_Controller {
  
       function __construct(){
        parent::__construct();
        $this->load->helper('url'); 
      }

      public function index() { 

         $this->load->helper(array('form'));
         $this->load->library('form_validation');
         $this->form_validation->set_rules('name', 'Name', 'required'); 
         
         if ($this->form_validation->run() == FALSE) { 
         $this->load->view('myform'); 
         } 
         else { 
            $this->load->view('formsuccess'); 
         } 
      }
   } 
?>

This form also shows errors if it is not correctly validated or forwarded to the “formsuccess.php” page.

In the “application/config/routes.php”, add the following line.

$route['validation'] = 'Form';

Let’s run this example by visiting the URL in the browser below. This URL may vary depending on your site.

https://tutorialpace.com/index.php/validation

The following screen will be produced.

CodeIgniter Form Validation

We added a validation in the controller − The name field is required before the form is submitted. Therefore, if you click the submit button without entering anything in the name field, you are asked to enter the name before submitting as shown in the following screen.

CodeIgniter Form ValidationAfter successfully entering your name, you are redirected to the screen as shown below.

CodeIgniter Form ValidationIn the example above, we used the required setting of rules. The CodeIgniter contains many rules, which are described below. For more details on Form Validation view document.

CodeIgniter Validation Rules

The following is a list of all available native rules.

Rules Parameters Description Example
required No Returns FALSE if the form is empty.
matches Yes Returns FALSE if the form element is not the same as the parameter element. matches[form_item]
regex_match Yes Returns FALSE if the element of the form does not match the normal expression. regex_match[/regex/]
differs Yes Returns FALSE if the form element is different than the parameter element. differs[form_item]
is_unique Yes Returns FALSE if the form element is not unique to the parameter ‘s table and field name. Note − This rule requires that the query builder is enabled for working. is_unique[table.field]
min_length Yes Returns FALSE if the form is shorter than the value of the parameter. min_length[3]
max_length Yes Returns FALSE if the form is longer than the value of the parameter. max_length[12]
exact_length Yes Returns FALSE if the parameter value is not the form element. exact_length[8]
greater_than Yes Returns FALSE if the form element is equal to or below the number value of the parameter. greater_than[8]
greater_than_equal_to Yes Returns FALSE if the element of the form is lower than or not numerical. greater_than_equal_to[8]
less_than Yes Returns FALSE if the form element is larger or equal to or not numeric to the parameter value. less_than[8]
less_than_equal_to Yes Returns FALSE if the form element is greater than or not numeric. less_than_equal_to[8]
in_list Yes Returns FALSE if the form element is not part of the default list. in_list[red,blue,green]
alpha No Returns FALSE if there are anything but alphabetical characters in the form element.
alpha_numeric No Returns FALSE if there is nothing but alphanumeric characters in the form element.
alpha_numeric_spaces No Returns FALSE if there are anything but alphanumeric characters or spaces in the form element. Should be used to avoid spaces at the beginning or end after trimming.
alpha_dash No Returns FALSE if there is anything but alphanumeric characters, underscores or dashes in the form element.
numeric No Returns FALSE if there is anything but numeric characters in the form element.
integer No Returns FALSE if there is anything but an integer in the form element.
decimal No Returns FALSE if there is nothing but a decimal number in the form element.
is_natural No Returns FALSE if the element of the form contains anything but a natural number-0, 1, 2, 3, etc.
is_natural_no_zero No Returns FALSE if the element of the form contains anything but a natural number, but not − 1, 2, 3, etc.
valid_url No Returns FALSE when there is no valid URL in the form element.
valid_email No Returns FALSE if there is no valid e – mail address in the form element.
valid_emails No Returns FALSE if any value in a list separated by a comma is not a valid email.
valid_ip No Returns FALSE if the IP is invalid. Accepts an optional ‘ ipv4’ or ‘ ipv6’ parameter to specify an IP format.
valid_base64 No Returns FALSE if there is anything but valid Base64 characters in the provided string.