Codeigniter自定义电子邮件验证规则

时间:2011-11-22 00:00:07

标签: php codeigniter validation email

有人可以就如何为Codeigniter创建自定义验证规则提供一些建议。

我正在处理的网站是针对大学生的,只允许用户在使用大学电子邮件地址时进行注册。

以下是我想要实现的验证类型的示例:

仅允许以:

结尾的电子邮件
array(
    '0' => '@uni-email-1.com',
    '1' => '@uni-email-2.com',
    '2' => '@uni-email-3.com',
    '3' => '@uni-email-4.com',
);

我仍然是codeigniter的新手,我不知道如何创建这种类型的验证。

非常感谢任何帮助!

由于

6 个答案:

答案 0 :(得分:11)

当然,这是怎么回事?

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;

}

答案 1 :(得分:4)

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;
    }

答案 2 :(得分:1)

这实际上与PHP有关,而不是CI。基本上,您应该将插入的电子邮件地址与您提供的每个结束字符串匹配。如果匹配 - 允许注册,如果没有 - 输出警报或其他内容但不允许注册。

答案 3 :(得分:1)

如果您的大学有LDAP目录,那么您可以对其进行身份验证,而不是拥有其他基础。 Single Sign On是一个非常好的系统,适用于大学等机构

答案 4 :(得分:1)

尝试以下内容,

$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;
}

希望有所帮助

答案 5 :(得分:0)

您可以通过html5简单代码验证电子邮件

带图案

相关问题