Php Codeigniter正则表达式与比较匹配
我有一个像这样的正则表达式-Php Codeigniter正则表达式与比较匹配,php,regex,codeigniter,Php,Regex,Codeigniter,我有一个像这样的正则表达式- $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)值 例如:值8809898V(根据表达式,这是 (正确) 我为一个单独的
$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)值
例如:值8809898V(根据表达式,这是
(正确)
我为一个单独的文本字段输入的另一个字段的值是-1993
根据该值,1993是错误的,1988应该是正确的
因为第一个值以88开头,另一个值以88结尾
如何使用CodeIgniter编写代码来实现这一点 您可以像建议的那样编写回调函数。以下是一个例子:
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');
除非你澄清,否则我想帮你是不可能的?请在帖子中澄清。使用回调函数比较值。