CI form_validation類實例應用

test controller:php

test.php:css

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

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

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

  $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格式驗證
  url


  if($this->form_validation->run() == false){
   //失敗時重載
   $this->index();orm

  }else{
   //驗證經過
   echo 'Seccessfull!';
  }
 }
}xml

 

views:htm

view/includes/template.php:utf-8

<?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>

相關文章
相關標籤/搜索