使用CodeIgniter和Pregmatch表单验证的自定义错误消息

时间:2018-02-12 06:40:12

标签: php

它不起作用,我该怎么做才能使它工作?

if (preg_match('(?=.*[a-z])(?=.*[A-Z]).*', $str)) 
{
    return TRUE;
}
else
{
    $this->form_validation->set_message('user_password1', 'Please provide a stronger password');
    return FALSE;
}

1 个答案:

答案 0 :(得分:0)

使用your own validation methods这样的东西

<?php

class Form extends CI_Controller {

        public function index()
        {
                $this->load->helper(array('form', 'url'));

                $this->load->library('form_validation');

                $this->form_validation->set_rules('password', 'Password', 'required|callback_password_check');


                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('myform');
                }
                else
                {
                        $this->load->view('formsuccess');
                }
        }

        public function password_check($str)
        {
                if (preg_match('(?=.*[a-z])(?=.*[A-Z]).*', $str))
                {
                    return TRUE;
                }
                else
                {
                    $this->form_validation->set_message('password_check' , 'Please provide a stronger password');
                    return FALSE;
                }
        }

}