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>
<html>
    <head>
        <title>Login form</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
    </head>
    <body>
        <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" />
                        <br/><br/>
                        Enter Your Password <br/>
                        <div class="text-danger"><?php echo isset($errors['pass'])?$errors['pass']:''; ?></div>
                        <input type="password" name="pass" class="form-control" />
                        <br/><br/>
                        <input type="submit" name="login" value="Login" class="form-control" />
                    </div>
                </form>
            </div>
        </div>
    </body>
</html>

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

<?php

class User extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
    }

    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.

https://howtowebcode.com/user/login