验证表格

时间:2012-12-17 19:44:12

标签: php codeigniter

我有一个表单,我使用jQuery ajax提交,并将其发送到名为submit的控制器函数,以验证并执行表单数据所需的任何其他任务。我试图找出当用户名不包含小写字母和数字时,我的表单验证库没有显示错误的原因。

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower');

表单提交后的POST值:

username    TestingUSER

编辑:

据我所知,它可以正常进入php服务器端。

PHP:

public function submit()
{
    $output_status = 'Notice';
    $output_title = 'Not Processed';
    $output_message = 'The request was unprocessed!';

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
    $this->form_validation->set_rules('remember', 'Remember Me', 'trim|xss_clean|integer');

    if ($this->form_validation->run() == TRUE)
    {

    }
    else
    {
        $output_status = 'Error';
        $output_title = 'Form Not Validated';
        $output_message = validation_errors();
    }

    echo json_encode(array('output_status' => $output_status, 'output_title' => $output_title, 'output_message' => $output_message));
}

编辑2:

基于谢赫回答。我收到的回复是“无法访问与您的字段名称对应的错误消息”。它确实说标题的表单未经验证,因此消息不起作用。

public function check_username($str)
{
    if (preg_match('#[0-9]#', $str) && preg_match('#[a-z]#', $str)) 
    {
        return TRUE;
    }
    $this->form_validation->set_message('username', 'This is not have an accepted value!');
    return FALSE;
}

编辑3:

我想要做的是让它报告有验证错误,但没有pnotify响应中的特定错误。但是我确实希望它在表单元素下显示特定的错误。

jQuery代码:

http://pastebin.com/1KehMJkh

登录表格:

http://pastebin.com/EfpBfbfN

1 个答案:

答案 0 :(得分:1)

我认为您可以在控制器中使用回调函数

public function check_username($str)
{
    if (preg_match('#[a-z0-9]#', $str)) {
        return TRUE;
    }
    $this->form_validation->set_message('check_username', 'This is not have an accepted value!');
    return FALSE;
}

username

的验证规则
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_check_username');

You may like this too

相关问题