【问题标题】:How to validate multiple date format in form validation using regex_match in Codeigniter如何在 Codeigniter 中使用 regex_match 在表单验证中验证多个日期格式
【发布时间】:2021-09-17 17:05:00
【问题描述】:

如何在 codeigniter 中验证多个日期格式

这是我的模式:

/^([1-9]|1[0-9]|2[0-9]|3[0-1])\/([1-9]|1[0-2])\/202[0-5]$/

Codeigniter 代码:

$this->form_validation->set_rules('mir_date', 'MIR Date', 'required|regex_match[/^([1-9]|1[0-9]|2[0-9]|3[0-1])\/([1-9]|1[0-2])\/202[0-5]$/]', [
            'required' => 'MIR Date',
            'regex_match' => 'Invalid MIR Date'
]);

模式应接受以下日期格式d/m/yyyydd/mm/yyyydd/m/yyyyd/mm/yyyy

【问题讨论】:

  • 你确定你的语法没有关闭吗? ->->? regex_match[//.../]?尝试$this->form_validation->set_rules('mir_date', 'MIR Date', 'required|regex_match[/^([1-9]|1[0-9]|2[0-9]|3[0-1])\/([1-9]|1[0-2])\/202[0-5]$/]', [ 作为第一行。如果它不起作用,可能是由于|,您需要将其声明为单独的函数或定义为数组。

标签: php regex codeigniter


【解决方案1】:

您可以在表单验证中使用回调函数,如下所示:-

set_rules('username', 'Username',   
'trim|required|xss_clean|callback__regex_check'); 

public function regex_check($str)
{
    if (preg_match("/^(?:'[A-Za-z](([\._\-][A-Za-z0-9])|[A-Za-z0-9])*[a-z0-9_]*')$/", $str))
    {
        $this->form_validation->set_message('regex_check', 'The %s field is not valid!');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多