Codeigniter Form Validation

Validation is an important part of any web application.It ensure that the data we are getting is as per our requirement and valid.Codeigniter has an inbuilt library to validate form which makes it very easy. In this post we will go through codeigniter inbuilt form validation.

Step 1 : Create Controller:

defined('BASEPATH') OR exit('No direct script access allowed');
class Validation  extends CI_Controller {

	public function __construct(){
        parent::__construct();


		 $this->load->library('form_validation'); 

    }

    public function index(){
             
		$this->load->view('form_validation');
    }       

    public function form_validate(){
      
		 
			$this->form_validation->set_rules('name', 'Name ', 'required');
			$this->form_validation->set_rules('email', 'Email ', 'required');
			$this->form_validation->set_rules('mobno', 'Mobile No ', 'required');
			if ($this->form_validation->run() == FALSE){
                       
				$this->load->view('form_validation');
            }else{
				
				echo "Form is submitted";
				echo "<pre>";
				print_r($this->input->post());
			}		  
		 
    }    
}

Step 2 : Create View file form_validation.php:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Ci Form Validation Example</title>
  <meta charset="utf-8">
  <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.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
  
</head>
<body>
 
<h1 class="text-success" align="center">CI Form Validation Example</h1><br>

<div class="container">    
<h4 class="text-danger"><?php echo validation_errors(); ?></h4>

<form class="form-horizontal" method="post" action="<?php echo site_url();?>/validation/form_validate">

            <div class="form-group">
            <label class="control-label col-sm-2" for="email">Name:</label>
            <div class="col-sm-10">
                <input type="text" class="form-control"  placeholder="Enter Name" name="name">
            </div>
            </div>
            
            <div class="form-group">
            <label class="control-label col-sm-2" for="email">Email:</label>
            <div class="col-sm-10">
                <input type="email" class="form-control"  placeholder="Enter email" name="email">
            </div>
            </div>
            <div class="form-group">
            <label class="control-label col-sm-2" for="pwd">Mobno:</label>
            <div class="col-sm-10">          
                <input type="number" class="form-control"  placeholder="Enter Mobile No" name="mobno">
            </div>
            </div>
             
            <div class="form-group">        
            <div class="col-sm-offset-2 col-sm-10">
                <input  type="submit" class="btn btn-success" name="submit" value="Submit">
            </div>
            </div>
  </form>

 </div>
</body>
</html>
Exit mobile version