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
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;
}
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;
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With