简体   繁体   English

Codeigniter regex_match与比较

[英]Codeigniter regex_match with Comparison

I have this regex_match like - 我有这样的regex_match-

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

It validate 9 number followed by one letter that is v, V, x or X. 它验证9个数字,后跟一个字母,即v,V,x或X。

I need to combine the above validation to an another field witch which can enter year like 1988.and validate (88) value in both fields at the same time. 我需要将上述验证与另一个字段结合起来,该字段可以输入类似于1988.的年份,并同时验证两个字段中的(88)值。

Ex : Value 880989898V (According to the expression this is correct) 例如:值880989898V (根据表达式,这是正确的)

Another field I am entering for a separate text field the value for it is - 1993 我为另一个文本字段输入的另一个字段的值是-1993

According to the value 1993 is wrong and 1988 should be correct 根据值1993年是错误的而1988年应该是正确的

because 1st value start with 88 and other value ends with 88. 因为第一个值以88开头,其他值以88结束。

How can I write a code using CodeIgniter to achieve this. 如何使用CodeIgniter编写代码来实现此目的。

You can Write a callback function like @tpojka suggested. 您可以编写建议使用类似@tpojka的回调函数。 Here is an example: 这是一个例子:

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;
    }
}

And of course I have mentioned only one validation flag for simplicity, realistically you would need something like: 当然,为了简单起见,我只提到了一个验证标志,实际上,您将需要以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM