【问题标题】:Codeigniter regex_match with ComparisonCodeigniter regex_match 与比较
【发布时间】:2015-09-11 15:49:29
【问题描述】:

我有这样的 regex_match -

$this->form_validation->set_rules('oper_nic', 'NIC', 'trim|required|min_length[10]|regex_match[/^[0-9]{9}[vVxX]$/]');

它验证 9 个数字后跟一个字母,即 v、V、x 或 X。

我需要将上述验证结合到另一个字段中,该字段可以输入像 1988 这样的年份。并同时验证两个字段中的 (88) 值。

Ex : Value 880989898V (根据表达式,这是 正确)

我正在为单独的文本字段输入另一个字段,其值为 - 1993

根据值1993是错误的,1988应该是正确的

因为第一个值以 88 开头,其他值以 88 结尾。

如何使用 CodeIgniter 编写代码来实现这一点。

【问题讨论】:

  • 除非你澄清,否则我想这将无法帮助你。
  • 你是什么意思@stribizhev
  • 不,你是什么意思?请在帖子中澄清。
  • 使用回调函数比较值。

标签: php regex codeigniter


【解决方案1】:

您可以编写一个回调函数,如建议的@tpojka。这是一个例子:

class Form extends CI_Controller {

    public function index()
    {
        // load stuff
        // ...

        $this->form_validation->set_rules('oper_nic', 'NIC', 'trim|required|min_length[10]|regex_match[/^[0-9]{9}[vVxX]$/]');
        $this->form_validation->set_rules('year', 'Year', 'callback_year_check');

        if ($this->form_validation->run() == FALSE)
        {
            // failure
        }
        else
        {
            // success
        }
    }

    public function year_check($str)
    {
        if (substr($str, -2) == substr($this->form_validation->set_value('oper_nic'), 0, 2)) 
        {
            return true;
        } 
        return false;
    }
}

当然,为了简单起见,我只提到了一个验证标志,实际上你需要这样的东西:

$this->form_validation->set_rules('year', 'Year', 'trim|required|regex_match[/^[0-9]{4}$/]|callback_year_check');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 2016-03-04
    • 1970-01-01
    • 2018-03-15
    • 2021-07-14
    相关资源
    最近更新 更多