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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8220253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:11:08  来源:igfitidea点击:

Codeigniter custom email validation rules

phpcodeignitervalidationemail

提问by RipzCurlz

Can someone please provide some advise on how to go about creating custom validation rules for Codeigniter.

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

The website I am working on is for university students and only allows users to register if they use their university email address.

我正在开发的网站是面向大学生的,并且只允许用户使用他们的大学电子邮件地址进行注册。

Here is an example of the type of validation I am trying to achieve:

这是我试图实现的验证类型的示例:

Only allow emails that end with:

只允许以以下结尾的电子邮件:

array(
    '0' => '@uni-email-1.com',
    '1' => '@uni-email-2.com',
    '2' => '@uni-email-3.com',
    '3' => '@uni-email-4.com',
);

I am still quite new to codeigniter and I am not sure how to create this type of validation.

我对 codeigniter 还是很陌生,我不确定如何创建这种类型的验证。

Any assistance is very much appreciated!

非常感谢任何帮助!

Thanks

谢谢

回答by Steve Lewis

Sure, how's this?

当然,这个怎么样?

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;

}

回答by rajangupta

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

回答by Shomz

This is actually more related to PHP than CI. Basically, you should string match inserted email address to each ending string you provided. If there is a match - allow registering, if not - output an alert or something and don't allow it.

这实际上比 CI 更与 PHP 相关。基本上,您应该将插入的电子邮件地址字符串匹配到您提供的每个结尾字符串。如果匹配 - 允许注册,如果不匹配 - 输出警报或其他内容并且不允许它。

回答by ciuser

If your university has an LDAP directory, then you can do authentication over it, instead of having another base. Single Sign On is a very good system for use in institutions such as universities

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

回答by Sudhir Bastakoti

Try something like following,

尝试以下操作,

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

Hope it helps

希望能帮助到你

回答by Raj Singh

You can validate email by html5 simple code

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

with pattern

有图案