Seach

form validation in codeigniter

set libarary form_validation in autoload.php




validation.php


<?php
defined('BASEPATH') OR EXIT('Dirrect script not allowed');
class Validation extends CI_Controller
{
   
function __construct() {
    parent::__construct();
}  
public function index()
{
    $this->load->view('Validation_view');
}
public function validate()
{
    $this->form_validation->set_rules('name','User Name','required',array('required'=>'please enter a user name'));
    $this->form_validation->set_rules('age','Age ','required|numeric');
    $this->form_validation->set_rules('email','Email','required|valid_email');
    $this->form_validation->set_rules('phoneno','Phone number','required|numeric|max_length[10]');
    $this->form_validation->set_rules('password','password','trim|md5|required');
    $this->form_validation->set_rules('rpassword','password','md5|trim|required|matches[password]');
    if($this->form_validation->run()==FALSE)
    {
        $this->load->view("validation_view");
    }
 else {
        echo 'no error';
    }
}
}
?>


validation_view.php


<?php echo validation_errors();?>
<form method="post" action="<?php echo site_url('validation/validate')?>">
    <table border="1">
        <caption><h1>Registeration</h1></caption>
        <tr>
            <td>
                <label>Name</label>
            </td>
            <td>
                <input type="text" name="name" value="<?php echo set_value('name'); ?>">
            </td>
        </tr>
        <tr>
            <td>
                <label>Age</label>
            </td>
            <td>
                <input type="text" name="age" value="<?php echo set_value('age');?>">
            </td>
        </tr>
       
         <tr>
            <td>
                <label>Email</label>
            </td>
            <td>
                <input type="text" name="email" value="<?php echo set_value('email');?>">
            </td>
        </tr>
        <tr>
            <td>
                <label>Phone No</label>
            </td>
            <td>
                <input type="text" name="phoneno" value="<?php echo set_value('phoneno');?>">
            </td>
        </tr>
       
        <tr>
            <td>
                <label>Password</label>
            </td>
            <td>
                <input type="text" name="password" value="<?php echo set_value('password');?>">
            </td>
        </tr>
       
        <tr>
            <td>
                <label>Retype Password</label>
            </td>
            <td>
                <input type="text" name="rpassword" value="<?php echo set_value('rpassword');?>">
            </td>
        </tr>
       
       
        <tr>
            <td>
                <input type="submit" name="save" value="Save">
            </td>
        </tr>
       
    </table>
</form>