Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 验证表单_Php_Codeigniter - Fatal编程技术网

Php 验证表单

Php 验证表单,php,codeigniter,Php,Codeigniter,我有一个使用jQueryAjax提交的表单,并将其发送到名为submit的控制器函数,以验证表单数据,并执行需要的任何其他任务。我试图找出为什么我的表单验证库在用户名不包含小写字母和数字时没有显示错误 $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower'); 提交表格后的发布值: username TestingUSER 编辑: 据我所知,

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

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower');
提交表格后的发布值:

username    TestingUSER
编辑:

据我所知,它正确地到达了php服务器端

PHP:

编辑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代码:

登入表格:


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

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;
}
用户名的验证规则

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

.

请显示实际的ajax/javascript代码和实际的控制器代码。没有ajax它能工作吗?@KevinSmith,字段名应该是
check\u username
,而不是
username
,即
$this->form\u validation->set\u message('check\u username','…'))
当从
回调函数设置它时。这是一个很好的答案,但是我很想知道为什么你有字母数字规则和回调函数。@KevinSmith,谢谢,刚刚添加了
字母数字
,但不是必需的。你能看看我的Edit 2响应吗。@KevinSmith,它应该是
check_username
而不是
username
作为
set_消息中的字段名
@KevinSmith,对不起!不熟悉
pnotify
,要打印特定的错误消息,您需要以这样的方式发送响应,即您可以找出哪个消息属于哪个元素。
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_check_username');