1

我想在 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
4

1 回答 1

2

您需要编写自定义验证规则。像这样的东西:

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');

该文档非常清楚地解释了这一切

于 2012-04-18T21:15:11.753 回答