user1056864
user1056864

Reputation: 131

CodeIgniter form validation - how to make it case insensitive

I am using Code Igniter 2, and the following code doesn't validate in case the user entered the same email address but in different cases, for example: [email protected] and [email protected] I want this to validate if it's the same email even if the user used different cases.

$this->form_validation->set_rules('password', 'Password', 'required|trim|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|trim');

if ($this->form_validation->run() == FALSE)
{

    $this->load->view('login_view', $data);
}
else
{

Upvotes: 2

Views: 2452

Answers (3)

Ben Swinburne
Ben Swinburne

Reputation: 26477

Any PHP function which accepts one parameter can be used in the validation class.

From the manual

Any native PHP function that accepts one parameter can be used as a rule, like htmlspecialchars, trim, MD5, etc.

Which means you can just amend your validation rule to

$this->form_validation->set_rules('email', 'Email', 'required|valid_email|strtolower|trim');

Note the use of the strtolower() function in the rules.

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

Do this as:

$this->form_validation->set_rules('email', 'Email', 'required|callback__check_email');

//then the callback function

function _check_email() {
  $email = strtolower($this->input->post("email");
  $email = trim($email);
  return $this->form_validation->valid_email($email);
}

Hope it helps

Upvotes: 0

Cubed Eye
Cubed Eye

Reputation: 5631

You need to create a callback function inside your controller:

function valid_case_insensitive_email($str){
    $str = strtolower($str)
    return $this->form_validation->valid_email($str)
}

Then modify the set_rules to this

$this->form_validation->set_rules('email', 'Email', 'required|callback_valid_case_insensitive_email|trim');

Alternatively you could extend the form_validation class and overwrite the valid_email function.

Upvotes: 0

Related Questions