Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter custom email validation rules

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

like image 785
RipzCurlz Avatar asked Nov 22 '11 00:11

RipzCurlz


4 Answers

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;

}
like image 80
Steve Lewis Avatar answered Nov 14 '22 20:11

Steve Lewis


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;
    }
like image 28
rajangupta Avatar answered Nov 14 '22 18:11

rajangupta


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.

like image 1
Shomz Avatar answered Nov 14 '22 19:11

Shomz


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

like image 1
ciuser Avatar answered Nov 14 '22 18:11

ciuser