How to Validate Form in Codeigniter

How to Validate Form in Codeigniter

In this post i will explain how to validate a form in codeIgniter, codeigniter provide a form validation class to minimize the code that are include in form validation at user level.

CodeIgniter form validation are simple approach to validate the data. In this process when you create a form that form is displayed and you fill the form and click the submit button, if you fill up correct and required data in appropriate field it displayed your desired data or go to another page it totally depends on what you want but if you fill up wrong way and not fill up required data in appropriate fields then form is displayed data with error message , this message simply tell you fill up the required fields that containing forms.

A forms contains many fields like this

  • Textbox
  • Textarea
  • Radio button
  • Select
  • Checkbox etc
CodegIniter provides followings Validation rules to validate the form, so you can apply on from, I will explain few most important rules that are mostly used in our project

required: It return false, if form element is empty, simply means that user does not fill up required fields
For example my form have Name field. Here you can apply required rule in name fields.

matches: It return false, if the form element does not match the one in parameter. You can use our form matches[form_field], for example I want to match confirm password field to password filed so you can use like matches[password].

min_lenght: It return false, if the form element is smaller then the parameter value for example I have a form that form contains a password filed and  I want to password should be not less that 8 character. You can use our form min_length[8].

max_length: It return false, if the form element is longer then the parameter value for example I have a form that form contains a first name filed and  I want to first name should be not more then that 15 character. You can use our form max_length[15].

exact_length: It return false, if the form element is not exactly the parameter value. You can use our form exact_length[15].

greater_than: It return false, if the form element is less than the parameter value or not numeric. You can use our form greater_than[6].

less_than: It return false, if the form element is greater than the parameter value or not numeric. You can use our form less_than[6].

alpha: It return false, if the form element contains anything other then  alphabetical characters.

alpha_numeric: It return false, if the form element contains anything other then alpha-numeric characters.

numeric: It return false, it return false, if the form element contains anything other then numeric characters.

integer: It return false, it return false, if the form element contains anything other then a integer.

valid_email: It return false, if the form element does not contains a valid email address.

is_natural: It return false, if the form element contains anything other then a natural numbers like 0, 1,2, 3, etc.

is_natural_no_zero: It return false, if the form element contains anything other then a natural numbers, but not zero, like 1,2, 3, etc.
 
For example: Here I m using some validation rules like alpha in name and username field for alphabetical characters, min_length[6] for password should be minimum 6 character long, matches[password] for match the password, numeric and greater_than[18] for  age should be numeric and more than 18 years, valid_emails for valid email address, integer and exact_length[10] for mobile no should be 10 digit long.

Create a View: validate_form.php, code shown below

<!DOCTYPE html>
  <head>
    <title>Form Validation in CodeIgniter</title>
  </head>
  <body>
    <form action="<?php echo site_url('Validation/form_validate'); ?>" method="post">
    
      <table align="center">
      <tr>
      <td style="color:red;"><?php echo validation_errors(); ?></td>
      </tr>
      <tr>
          <td>Name:</td>
          <td><?php echo form_input(array('id'=>'name', 'name'=>'name', 'placeholder' => 'Name', 'size'=>25, 'value'=>set_value('name')));?></td>
        </tr>
        <tr>
          <td>Username:</td>
          <td><?php echo form_input(array('id'=>'username', 'name'=>'username', 'placeholder' => 'Username', 'size'=>25, 'value'=>set_value('username')));?></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><?php echo form_input(array('id'=>'password', 'name'=>'password', 'placeholder' => 'Password', 'size'=>25, 'value'=>set_value('password')));?></td>
        </tr>
        <tr>
          <td>Confirm Password:</td>
          <td><?php echo form_input(array('id'=>'cpassword', 'name'=>'cpassword', 'placeholder' => 'Confirm Password', 'size'=>25, 'value'=>set_value('cpassword')));?></td>
        </tr>  
        <tr>
          <td>Age:</td>
          <td><?php echo form_input(array('id'=>'age', 'name'=>'age', 'placeholder' => 'Age', 'size'=>25, 'value'=>set_value('age')));?></td>
        </tr>
        <tr>
          <td>Email:</td>
          <td><?php echo form_input(array('id'=>'email', 'name'=>'email', 'placeholder' => 'Email', 'size'=>25, 'value'=>set_value('email')));?></td>
        </tr>
        <tr>
          <td>Mobile No:</td>
          <td><?php echo form_input(array('id'=>'mobile', 'name'=>'mobile', 'placeholder' => 'Mobile No', 'size'=>25, 'value'=>set_value('mobile')));?></td>
        </tr>
        <tr>
          <td></td>
          <td><button type="submit" id="employee-submit">Submit</button>
         </td>
        </tr>      
      </table>
    </form>
   </body>
  </html>

Again create another view: success_form.php, code shown below

<!DOCTYPE html>
  <head>
    <title>Success Page Form Validation</title>
  </head>
  <body>
    <h1>Form Data Successfully Submitted...</h1>
  </body>
</html>

Now create a Controller: validation.php, code shown below

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Validation extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));

// Load form validation library
$this->load->library('form_validation');

}

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

public function form_validate()
{
$this->form_validation->set_rules('name', 'Name', 'required|alpha');
$this->form_validation->set_rules('username', 'Username', 'required|alpha');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|matches[password]');
$this->form_validation->set_rules('age', 'Age', 'required|numeric|greater_than[18]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_emails');
$this->form_validation->set_rules('mobile', 'Mobile No', 'required|integer|exact_length[10]');

if ($this->form_validation->run() == FALSE)
{
$this->load->view('validate_form');
}
else
{
 $this->load->view('success_form');
}
}
}


?>


In controller, first I will add ‘form_validation’ library, code shown below

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

This form_validation library set some validation rules for particular fields by using the set_rules() function, code shown below

$this->form_validation->set_rules(parameters);

Above function set_rules() takes three parameters

  • Field name
  • Field name to identify that particular filed
  • Validation rules

You can used validation rules like ,  code shown below

$this->form_validation->set_rules('password', 'Password, 'required');

Third parameter can be set of multiple validation rules like, code shown below


$this->form_validation->set_rules('password', Password', 'required|min_length[6]');

Now go to browser and type localhost/CIProject/Validation/form_validate and run, shown below


Validate Form in Codeigniter

If you click submit button without fill up any data you will get the error message, screen shown below


Validate Form in Codeigniter

If you click submit button after filled up all the fields but data are not right way, so you will get error message screen show below


Validate Form in Codeigniter

If you click submit button after correct fill up in the all fields you will get message screen shown below


Validate Form in Codeigniter




                       I hope, it helped you to understand, How to  Validate a Form in CodeIgniter.......Thanks

About MSK TUTORIAL

Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. http://www.msktutorial.com/ was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Thanks For Visiting Blog...