如何在 codeigniter 验证中允许空格、逗号、点和字母

rob*_*ins 3 php validation codeigniter

在我的视图页面中。我有一个用于输入评论的文本框。在我的 codeigniter 验证中,只允许使用 alpha。

我需要在评论字段中允许空格、逗号、点、连字符.. 如何在我的验证中设置规则

    $this->form_validation->set_rules('e_comment', 'Comments', 'required|alpha');
Run Code Online (Sandbox Code Playgroud)

Jee*_*usu 5

要进行自定义验证,您需要使用回调函数。

// validation rule
$this->form_validation->set_rules('comment', 'Comments', 'required|callback_customAlpha');

// callback function
public function customAlpha($str) 
{
    if ( !preg_match('/^[a-z .,\-]+$/i',$str) )
    {
        return false;
    }
}

// custom error message
$this->form_validation->set_message('customAlpha', 'error message');
Run Code Online (Sandbox Code Playgroud)