本文实例讲述了CI框架表单验证的方法。分享给大家供大家参考,具体如下:
2、设置验证规则(controller)
PHP;">
'username','label' => '用户名','rules' => 'required'
),array(
'field' => 'password','label' => '密码',array(
'field' => 'passconf','label' => '确认密码','rules' => 'required|matches[password]'
),array(
'field' => 'tel','label' => '手机','rules' => 'required|integer|exact_length[11]'),array(
'field' => 'email','label' => '邮箱','rules' => 'required|valid_email'
)
);
//上面的会自动
//单独设置规则
$this->form_validation->set_rules('username','Username','trim|required|min_length[5]|max_length[12]|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|matches[passconf]|md5');
$this->form_validation->set_rules('passconf','Password Confirmation','trim|required');
$this->form_validation->set_rules('email','Email','trim|required|valid_email');
?>
PHP;">
form_validation->set_message('required','必须填写');
$this->form_validation->set_message('valid_email','不是有效的email');
?>
4、运行检查错误信息(controller)
PHP;">
load->helper(array('form','url'));
//加载CI表单验证库
$this->load->library('form_validation');
//----------------------------------------
# 验证规则及错误信息代码放在这里
//----------------------------------------
if ($this->form_validation->run() == FALSE){
//提交失败 重新加载表单部分
$this->load->view('myform');
}else{
//提交成功 表单处理
//跳转成功页面
$this->load->view('formsuccess');
}
}
PHP;">
form_validation->set_error_delimiters('');
//设置成内联元素比较好
?>
6、错误后 重新回填表单(view)
PHP;">