我有这样的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:值880989898V(根据表达式,这是正确的)
我为另一个文本字段输入的另一个字段的值是-1993
根据值1993年是错误的而1988年应该是正确的
因为第一个值以88开头,其他值以88结束。
如何使用CodeIgniter编写代码来实现此目的。
您可以编写一个建议使用类似@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');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句