Codeigniter Server Side Form Validation With Error Message

Codeigniter Server Side Form Validation With Error Message

In this codeigniter server side form validation tutorial, here you will learn how to implement server side form validation with example. Basically validation happens on both side like server side and client side, In server side that you need to define the validation rules, error messages.

In this codeigniter form validation example, we will use form validation library of codeigniter that is validate form data on server side.

Basically in php codeigniter server side validation is most secure way than client side form validation, beacuse client side form validation can be bypass through script.

We provide demo link at the end of article. Checkout the link.

Codeigniter Server Side Form Validation


  • Download Codeigniter Latest
  • Basic Configurations
  • Create Database With Table
  • Setup Database Credentials
  • Make New Controller
  • Create Views
  • Start Development server
  • Conclusion

Download Codeigniter Project

In this step we will download the latest version of Codeigniter, Go to this link Download Codeigniter download the fresh setup of codeigniter and unzip the setup in your local system xampp/htdocs/ . And change the download folder name “demo”

Basic Configurations

Next we will set the some basic configuration on config.php file, so let’s go to application/config/config.php and open this file on text editor.

Set Base URL like this

$config['base_url'] = 'http://localhost/demo/';

Create Database With Table

In this step, we need to create database name demo, so let’s open your phpmyadmin and create the database with the name demo . After successfully create a database, you can use the below sql query for creating a table in your database.

    id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
    first_name varchar(100) NOT NULL COMMENT 'Name',
    last_name varchar(100) NOT NULL COMMENT 'Name',
    email varchar(255) NOT NULL COMMENT 'Email Address',
    contact_no varchar(50) NOT NULL COMMENT 'Contact No',
    created_at varchar(20) NOT NULL COMMENT 'Created date',
    PRIMARY KEY (id)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=1;

Setup Database Credentials

In this step, We need to connect our project to database. we need to go application/config/ and open database.php file in text editor. After open the file in text editor, We need to setup database credential in this file like below.

$db['default'] = array(
  'dsn' => '',
  'hostname' => 'localhost',
  'username' => 'root',
  'password' => '',
  'database' => 'demo',
  'dbdriver' => 'mysqli',
  'dbprefix' => '',
  'pconnect' => FALSE,
  'db_debug' => (ENVIRONMENT !== 'production'),
  'cache_on' => FALSE,
  'cachedir' => '',
  'char_set' => 'utf8',
  'dbcollat' => 'utf8_general_ci',
  'swap_pre' => '',
  'encrypt' => FALSE,
  'compress' => FALSE,
  'stricton' => FALSE,
  'failover' => array(),
  'save_queries' => TRUE

Create Controller

Now we need to create a controller name Users.php. In this controller we will create some method/function. We will build some of the methods like :

  • Index() – This is used to display a form.
  • post_validate() – This is used to validate data on server side and store a data into database.
class Users extends CI_Controller {
    public function __construct()
    public function index()
    public function post_validate()

        $this->form_validation->set_rules('first_name', 'First Name', 'required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'required');
        $this->form_validation->set_rules('contact_no', 'Contact No', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_message('required', 'Enter %s');

        if ($this->form_validation->run() === FALSE)
          $data = array(
             'first_name' => $this->input->post('first_name'),
             'last_name' => $this->input->post('last_name'),
             'contact_no' => $this->input->post('contact_no'),
             'email' => $this->input->post('email'),

            $insert = $this->db->insert('users', $data);
            if ($insert) {
            } else {
               redirect( base_url('users') ); 



Create Views

Now we need to create form_validation.php, go to application/views/ folder and create form_validation.php file. Here put the below html code for showing form.

<!DOCTYPE html>
  <title>Codeigniter Form Validation</title>
 <link rel="stylesheet" href="">
 <div class="container">
    <div class="row">
    <div class="col-md-9">
    <form action="<?php echo base_url('users/post_validate') ?>" method="post" accept-charset="utf-8">
      <div class="form-group">
        <label for="formGroupExampleInput">First Name</label>
        <input type="text" name="first_name" class="form-control" id="formGroupExampleInput" placeholder="Please enter first name">
        <?php echo form_error('first_name'); ?> 

      <div class="form-group">
        <label for="formGroupExampleInput">Last Name</label>
        <input type="text" name="last_name" class="form-control" id="formGroupExampleInput" placeholder="Please enter last name">
        <?php echo form_error('last_name'); ?>  

      <div class="form-group">
        <label for="email">Email Id</label>
        <input type="text" name="email" class="form-control" id="email" placeholder="Please enter email id">
        <?php echo form_error('email'); ?> 

      <div class="form-group">
        <label for="mobile_number">Mobile Number</label>
        <input type="text" name="contact_no" class="form-control" id="contact_no" placeholder="Please enter mobile number" maxlength="10">
        <?php echo form_error('contact_no'); ?> 

      <div class="form-group">
       <button type="submit" id="send_form" class="btn btn-success">Submit</button>
    <div class="col-md-3">

Now we need to create success.php file, so go to application/views/ and create success.php file. And put the below code here.

<!DOCTYPE html>
  <title>Codeigniter Form Validation</title>
 <link rel="stylesheet" href="">
<div class="container">
 <h1> Thank You for Registered</h1>

Start Development server

For start development server, Go to the browser and hit below the url.



In this codeigniter server side form validation tutorial, We have successfully validate form data on server side. After successfully validate data on server side, we send to users on success page.

You may like

  1. Registrtion form validation using jquery validator
  2. jQuery Form Validation Custom Error Message
  3. jQuery AJAX Form Submit PHP MySQL
  4. Simple Registration Form in PHP with Validation
  5. jQuery Ajax Form Submit with FormData Example
  6. Laravel 6 Ajax Form Submit With Validation Tutorial
  7. Google ReCaptcha Form Validation in laravel
  8. Laravel 6 | jQuery Form Validation Tutorial with Demo Example
  9. Form Validation Example In Laravel 6
  10. Codeigniter php jQuery Ajax Form Submit with Validation

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Codeigniter, Vue JS, Angular JS, React Js, WordPress and Bootstrap from a starting stage.

One reply to Codeigniter Server Side Form Validation With Error Message

  1. how to validate on modal

Leave a Reply

Your email address will not be published. Required fields are marked *