test controller:

test.php:

<?php
class Test extends CI_Controller{
 
 function __construct(){
  
  parent::__construct();
  $this->load->helper('url');
  $this->load->helper('form');
 }

 function index(){
  
  $this->load->library('form_validation');
  $data['main_content'] = 'form_check';
  $this->load->view('includes/template',$data);
 }

 function check_form(){
  
  $this->load->library('form_validation');

  $this->form_validation->set_rules('username','Username','trim|required|min_length[4]|max_length[12]');//username 要4个到12个字符之间
  $this->form_validation->set_rules('password','Password','trim|required|min_length[8]|max_length[12]');//password 要8个到12个字符之间
  $this->form_validation->set_rules('password2','Password Confirm','required|matches[password]');//两次密码输入必需一致
  $this->form_validation->set_rules('email','Email Address','required|valid_email');//Email格式验证
  


  if($this->form_validation->run() == false){
   //失败时重载
   $this->index();

  }else{
   //验证通过
   echo 'Seccessfull!';
  }
 }
}

 

views:

view/includes/template.php:

<?php $this->load->view('includes/header.php'); ?>

<?php $this->load->view($main_content); ?>

<?php $this->load->view('includes/footer.php'); ?>

view/includes/header.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="<?php echo base_url(); ?>css/style.css" rel="stylesheet" type="text/css" />
<title></title>
</head>
<body>

view/includes/footer.php:

</body>
</html>

view/form_check.php:

<div id='login_form'>
<h3>Login page:</h3>
<p>
<?php
echo form_open('test/check_form');
echo form_input('username',set_value('username','Username'));
echo form_password('password',set_value('password','Password'));
echo form_password('password2',set_value('password2','Password Confirm'));
echo form_input('email',set_value('email','Email Address'));
echo form_submit('sub','Submit');
?>
</p>
<?php echo validation_errors("<p style='color:red;font-weight:bold;'>"); ?>
</div>