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

发布于 2024-12-17 11:01:14 字数 391 浏览 0 评论 0原文

有人可以提供一些关于如何为 Codeigniter 创建自定义验证规则的建议吗?

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

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

仅允许以以下内容结尾的电子邮件:

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

我对 codeigniter 仍然很陌生,并且不确定如何创建这种类型的验证。

非常感谢任何帮助!

谢谢

Can someone please provide some advise on how to go about creating custom validation rules for 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.

Any assistance is very much appreciated!

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你与清晨阳光 2024-12-24 11:01:14

当然可以,这个怎么样?

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;

}

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;

}
冷默言语 2024-12-24 11:01:14
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;
    }
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;
    }
倾城泪 2024-12-24 11:01:14

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

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.

ゃ人海孤独症 2024-12-24 11:01:14

如果您的大学有 LDAP 目录,那么您可以通过它进行身份验证,而不需要使用另一个基地。单点登录是一个非常适合在大学等机构中使用的系统

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

娇纵 2024-12-24 11:01:14

尝试以下类似的方法,

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

希望有帮助

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

‖放下 2024-12-24 11:01:14

html5 简单代码来验证电子邮件

您可以通过带有模式的

You can validate email by html5 simple code

with pattern

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文