CodeIgniter provides a comprehensive form validation and data prepping class that helps minimize the amount of code you will write.Validation is an important process while building web application.

Create a view file login.php and save the below code it in application/views/login.php. The login.php page will display form where user can submit his name and we will validate this page to ensure that it should not be empty while submitting.

<!DOCTYPE html>
        <title>Login form</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="">
        <script src=""></script>
        <script src=""></script>
        <div class="container">
            <div class="row">
                <form method="post">
                    <div class="col-md-6">
                        <h3 class="text-center"><?php echo isset($errors['invalid']) ? $errors['invalid'] :''; ?></h3>
                        Enter Your Email<br/>
                        <div class="text-danger"><?php echo isset($errors['email'])?$errors['email']:''; ?></div>
                        <input type="text" name="email"class="form-control" />
                        Enter Your Password <br/>
                        <div class="text-danger"><?php echo isset($errors['pass'])?$errors['pass']:''; ?></div>
                        <input type="password" name="pass" class="form-control" />
                        <input type="submit" name="login" value="Login" class="form-control" />

Create a controller file User.php and save it in application/controller/User.php.


class User extends CI_Controller {

    public function __construct() {

    public function login() {
        $this->form_validation->set_rules('email', 'Email Id', 'trim|required|valid_email');
        $this->form_validation->set_rules('pass', 'Password', 'trim|required');
        $data['errors'] = array();
        if ($this->form_validation->run() == FALSE) {
            $error_messages = $this->form_validation->error_array();
            $data['errors'] = $error_messages;
        $this->load->view('login', $data);


Let us execute this example by the following URL in the browser. This URL may be different based on your website.