【问题标题】:YYYY-mm-dd Form validation in CodeigniterCodeigniter 中的 YYYY-mm-dd 表单验证
【发布时间】:2015-03-30 10:52:21
【问题描述】:

如何在 Codeigniter 中完成日期表单验证,

我试过这样,

$this->form_validation->set_rules('pastdate_start', 'Date of birth',
     'regex_match[(0[1-9]|1[0-9]|2[0-9]|3(0|1))-(0[1-9]|1[0-2])-\d{4}]');

但我得到了

function Preg-match 没有找到结束匹配分隔符')'

这些错误,如何解决,请给我解决方案。

我的编码有什么错误?

【问题讨论】:

    标签: codeigniter formvalidation-plugin


    【解决方案1】:

    试试这个..

    $this->form_validation->set_rules('pastdate_start','Date of birth','required|regex_match[/^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.]((?:19|20)\d\d),pastdate_start');
    

    【讨论】:

    • 它的 CodeIgniter 问题.. 因为我们使用 ` | ` 在 preg 匹配模式中,codeIgniter 将其视为验证器之间的分隔符,例如 required|valid_email。但我认为你应该为验证创建一个回调函数。
    • $this->form_validation->set_rules('dob','Date of birth','required|callback_regexMatch[]'); $this->form_validation->set_message('regexMatch', '%s is not a valid.'); 和回调函数会喜欢function regexMatch($str){ if (!preg_match("/^(0[1-9]|1[0-9]|2[0-9]|3(0|1))-(0[1-9]|1[0-2])-\d{4}$/",$str)) { return FALSE; } return TRUE; }
    • 嗨 Manish,感谢您的回复,在上面的编码中,仅检查字段是否为空,但无法检查 YYYY-MM-DD 格式 Y?我在库文件夹中的表单验证中编写了该函数 regxmatch正确与否?
    • 不要在表单验证中编写该函数。只需在验证表单的同一控制器中编写回调函数。 link 使用此链接并找到 Callbacks: Your own Validation Functions
    猜你喜欢
    • 2013-07-06
    • 2015-10-30
    • 1970-01-01
    • 2014-04-30
    • 2017-04-07
    • 2011-02-12
    • 2022-11-29
    • 2020-02-24
    相关资源
    最近更新 更多