CI 配置验证规则
//判断表单域,提交表单显示对应的错误信息
$this->load->library('form_validation');
$config = array(
array(
'field' => 'username',
'label' => '用户名',
'rules' => 'trim|required|min_length[2]|max_length[12]' //|xss_clean'
), //将用户名通过“xss_clean”函数处理去掉了有害数据。
array(
'field' => 'password',
'label' => '密码',
'rules' => 'trim|required|md5'
),
array(
'field' => 'passconf',
'label' => '再次输入',
'rules' => 'trim|required|md5|matches[password]'
),
array(
'field' => 'email',
'label' => '邮箱',
'rules' => 'trim|required|valid_email'
)
);
$this->form_validation->set_rules($config);
$bool = $this->form_validation->run();
//-----------------------------------------------------------------------------------------------------
// 第二种规则定义
// $this->load->library('form_validation');
// $this->form_validation->set_rules('username', '用户名', 'required|min_length[5]|max_length[12]');
// $this->form_validation->set_rules('password', '密码', 'required|matches[passconf]');
// $this->form_validation->set_rules('passconf', '再次输入', 'required');
// $this->form_validation->set_rules('email', '邮箱', 'required|valid_email');
//$bool = $this->form_validation->run();
//-----------------------------------------------------------------------------------------------------
单独生成一个 form_validation.php 文件,放到【application\config】文件夹下:
内容为:
<?php
/*
* 配置验证规则
*/
$config = array(
'zhuce' => array(
array(
'field' => 'username',
'label' => '用户名',
'rules' => 'trim|required|min_length[2]|max_length[12]|callback_username_check' //|xss_clean'
), //将用户名通过“xss_clean”函数处理去掉了有害数据。
array(
'field' => 'password',
'label' => '密码',
'rules' => 'trim|required|md5'
),
array(
'field' => 'passconf',
'label' => '再次输入',
'rules' => 'trim|required|md5|matches[password]'
),
array(
'field' => 'email',
'label' => '邮箱',
'rules' => 'trim|required|valid_email'
)
),
'login' => array(
array(
'field' => 'username',
'label' => '用户名',
'rules' => 'trim|required|min_length[2]|max_length[12]' //|xss_clean'
), //将用户名通过“xss_clean”函数处理去掉了有害数据。
array(
'field' => 'password',
'label' => '密码',
'rules' => 'trim|required|md5'
)
)
);
调用时:
$this->load->library('form_validation');
$bool = $this->form_validation->run('login');
if ($bool == FALSE) {
// 返回到注册页面
}else{
//验证没有问题
}