Welcome Guest, Not a member yet? Register   Sign In
Validate Email within Custom Callback?
#1

[eluser]Corey Freeman[/eluser]
Another registration problem...how do I check if an email is valid or not? I'm using the email helper but I can't seem to integrate it into the function properly...

Code:
function email_check($str) {
        //This field is required and cannot be blank.
        if ($str == '') {
            $this->form_validation->set_message('email_check', 'The Email Field is Required');
            return FALSE;
        }
        //Is the email valid?
        $this->load->helper('email');
        elseif (!valid_email($str)) {
            $this->form_validation->set_message('email_check', 'Please Enter a Valid Email Address');
            return FALSE;
        }
        //Does the email already exist in the database? That's a no no.
        $query = $this->db->get('players');
        foreach ($query->result() as $row):
        $email_exist = $row->email_address;
        endforeach;
        elseif ($str == $email_exist)
        {
            $this->form_validation->set_message('email_check', 'That Email Already Exists');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

Instead of checking to see if it's valid, I want to check and see if it's invalid. Is there a way to do that?
#2

[eluser]mddd[/eluser]
I think you are making things a little more complicated than neccessary. Remember you can have multiple validation callbacks on a field. In your code, you are checking to see if the email address isn't empty, is a real address, and doesn't already exist in the database.

The first two are normal callbacks you can do outside of your custom function:
Code:
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
This will do your first checks for you!

Then the last one: checking in the database. In your code, you load ALL your users and then say " $email_exist = $row->email_address " for every one of them. That doesn't actually DO anything! After the foreach you check against $email_exist. That means you only check if the address is the same as the last one in your database..

You should make your rule like this:
Code:
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_new_email');
And make a 'new_email' callback like this:
Code:
function new_email($str)
{
  // return true if the address is indeed a new address
  $this->db->where('email_address', $str);
  $found = $this->db->get('players')->num_results(); // this returns the number of rows having the same address.
  if ($found!=0)
     return false;  // more than 0 rows found. the callback fails.
  else
    return true;   // 0 rows found. callback is ok.
}
#3

[eluser]Corey Freeman[/eluser]
Haha thanks for explaining it so clearly! That's a lot easier than the string of if statements! Thanks for your help. Smile

EDIT(again): Fixed it! Thanks again! Here's the final function I ended up with:

Code:
function new_email($str) {
        $this->db->where('email_address', $this->input->post('email_address'));
        $query = $this->db->get('players');
        if($query->num_rows == 1)
        {
        $this->form_validation->set_message('new_email', 'That Email Address Already Exists');
        return false;
        }
        else {
        return true;
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB