zoukankan      html  css  js  c++  java
  • CI form_validation类实例应用

    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>

  • 相关阅读:
    Opencv:视频中人脸检测并保存人脸图片
    Opencv:图片中检测人脸并保存
    Opencv:按帧数读取视频并保存图片
    《软件工程》学习进度博客9
    第一阶段冲刺4
    02构建之法阅读笔记2—到底几个人开发?
    第一阶段冲刺3
    第一阶段冲刺2
    第一阶段冲刺1
    《软件工程》结对作业2----顶会热词统计
  • 原文地址:https://www.cnblogs.com/afei-happy/p/3407120.html
Copyright © 2011-2022 走看看