谁能提供一些建议,如何去创建自定义验证规则Codeigniter
我正在工作的网站是为大学生,只有允许用户使用他们的大学电子邮件地址注册。
下面是我试图实现的验证类型的一个示例:
只允许以:
结尾的邮件array(
'0' => '@uni-email-1.com',
'1' => '@uni-email-2.com',
'2' => '@uni-email-3.com',
'3' => '@uni-email-4.com',
);
我对codeigniter仍然很陌生,我不确定如何创建这种类型的验证。
非常感谢任何帮助!
谢谢
当然可以,这个怎么样?
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('email', 'Email', 'valid_email');
$this->form_validation->set_rules('email', 'Email', 'callback_email_check');
if ($this->form_validation->run() == FALSE)
{
//fail
}
else
{
//success
}
}
function email_check($str)
{
if (stristr($str,'@uni-email-1.com') !== false) return true;
if (stristr($str,'@uni-email-2.com') !== false) return true;
if (stristr($str,'@uni-email-3.com') !== false) return true;
$this->form_validation->set_message('email', 'Please provide an acceptable email address.');
return FALSE;
}
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|callback_email_check');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('form');
}
else
{
$this->load->view('formsuccess');
}
}
public function email_check($email)
{
if(stristr($str,'@uni-email-1.com') !== false) return true;
if(stristr($str,'@uni-email-2.com') !== false) return true;
if(stristr($str,'@uni-email-3.com') !== false) return true;
$this->form_validation->set_message('email', 'Please provide an acceptable email address.');
return FALSE;
}
这实际上与PHP比CI更相关。基本上,您应该将插入的电子邮件地址与您提供的每个结束字符串进行字符串匹配。如果有匹配-允许注册,如果没有-输出警告或其他东西,不允许注册
如果您的大学有一个LDAP目录,那么您可以在它上面进行身份验证,而不是有另一个基地。单点登录是一个非常适合在大学等机构使用的系统
尝试如下内容,
$formatArr = array("@uni-email-1.com", "@uni-email-2.com" "@uni-email-3.com");
$this->form_validation->set_rules('email', 'Email', 'callback__check_email');
//then the validation function
function _check_email() {
$email = $this->input->post("email");
$emailLastPart = array_pop(explode("@", $email));
if(!in_array($emailLastPart, $formatArr)) {
$this->form_validation->set_message('email', 'Please provide valid email address.');
return FALSE;
}
return TRUE;
}
希望有所帮助
您可以通过html5简单代码验证电子邮件
与模式