php代码: //注册 public function register(){ $this->load->helper('form'); //切换中文表单验证语言包 $this->config->set_item('language', 'chinese');//设置语言 $this->load->library('form_validation'); $this->form_validation->set_rules('phone','手机号','required|numeric|exact_length[11]|callback_phone_check'); $yz_status=$this->form_validation->run(); if($yz_status){//验证通过后保存 //验证通过代码 }else{//验证未通过 //显示模板提示错误 } }
//注册时验证手机号是否存在 public function phone_check($str){ $isExistUser = $this->UsersModel->get_one(array('phone'=>$str),'uid desc'); if ($isExistUser){ $this->form_validation->set_message('phone_check', '{field}已存在'); return FALSE; }else{ return TRUE; } }
模板代码:
<div style="line-height: 20px;height: 26px;" id="ck_phone"> {form_error('phone', '<div class="Tips">', '</div>')} </div>