php
$this->formvalidation->setrules(phone, 手机号, callbackcheckphone);public function checkphone($phone) { if (pregmatch(/^1[3-9]\d{9}$/, $phone)) {
return TRUE;
} else {
$this->formvalidation->setmessage(check_phone, 请输入正确的手机号码);
return FALSE;
}
}回调函数必须返回布尔值,可将验证规则写入配置文件以提高可维护性。三角洲行动无后座创建config/form_validation.php:
php $config = array( user/register => array( array( field => username, label => 用户名, rules => required|min_length[5] ), array( field => email, label => 邮箱, rules => required|valid_email ) ) );
然后在控制器中调用