我想在codeigniter中设置验证规则,例如字段以字符'P'或'S'开头,否则它无效。我怎么能用Codigniter验证库做到这一点?
Test Case 1: input: A145874 ------- invalid Must start with P or S
Test Case 2: input: P258741 ------- valid
Test Case 3: input: P45KK91 ------- invalid Must not contain Letters in other positions rather the first one.
Test Case 4: input: S457821 ------- valid
答案 0 :(得分:1)
您需要编写自定义验证规则。像这样:
public function check_first_char($str) {
$first_char = substr($str, 0, 1);
if ($first_char != 'P' || $first_char != 'S') {
$this->form_validation->set_message('check_first_char', 'The %s field must begin with P or S!');
return FALSE;
} else {
return TRUE;
}
}
然后你会像这样添加验证规则:
$this->form_validation->set_rules('field_name', 'Field Name', 'callback_check_first_char');