How to use form validation library in codeigniter

Form validation is required in all the form wherever you get inputs from user. Server side validation is more secure than client side validation, because user could disable javascript by blocking javascript from browser.

You can define validation using form validation library in codeigniter.

I am going to explain you how to use form validation library in codeigniter.In this tutorial my app name is myapp

1.) First of all you need to load form helper and form_validation library inside routes.php .see below.

$autoload[‘libraries’] = array(‘database’,’form_validation’);
$autoload[‘helper’] = array(‘url’,’html’,’file’,’form’);

2.) Next, Create a view by running below script.


<html>
<head>
<title>Registration</title>
</head>
<body>
<style>
h3
{
font-family: Verdana;
font-size: 18pt;
font-style: normal;
font-weight: bold;
color:red;
text-align: center;
}
table{
font-family: Verdana;
color:black;
font-size: 12pt;
font-style: normal;
font-weight: bold;
text-align:left;
border-collapse: collapse;
}
.error{
color:red;
font-size: 11px;
}
</style>
<h3>Registration</h3>
<?php echo form_open('register',array('name'=>'registerview')); ?>
<table align="center" cellpadding = "5">
<tr>
<td>Name</td>
<td><input type="text" size="40px" name="name" /></td>
<td class="error"><?php echo form_error('name'); ?></td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" size="40px" name="mobile" maxlength="10"/></td>
<td class="error"><?php echo form_error('mobile'); ?></td>
</tr>
<tr>
<td>Adress</td>
<td><input type="text" size="40px" name="address" maxlength="100"/></td>
<td class="error"><?php echo form_error('address'); ?></td>
</tr>
<tr>
<td colspan="5" align="center">
<input type="submit" value="Register"/></td>
</tr>
</table>
<?php echo form_close();?>
</body>
</html>

3.) Now create a controller and all form validation process should goes here as i am showing below.
RegisterController.php


<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class RegisterController extends CI_Controller {
public function index()
{
$this->load->view('registerview');
}
public function register()  {
$this->form_validation->set_rules('name','Name','trim|required|max_length[50]');
$this->form_validation->set_rules('mobile','Mobile','trim|required|exact_length[10]');
$this->form_validation->set_rules('address','Address','trim|required');
if($this->form_validation->run() == false){
$this->load->view('registerview');
}
else{
$this->load->model('registermodel');
$data = array(
'name'=>$this->input->post('name'),
'mobile'=>$this->input->post('mobile'),
'address'=>$this->input->post('address')
);
$this->registermodel->save($data);
redirect('registercontroller');
}
}
}

4.) After done above now go to browser and type http:\\localhost\myapp and try to submit your form without any input.You will get below screen.

Form validation using codeigniter library
Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – let us know. We’d love to help!

 

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.