Validate Email within Custom Callback? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Validate Email within Custom Callback? (/showthread.php?tid=31868) |
Validate Email within Custom Callback? - El Forum - 07-05-2010 [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) { 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? Validate Email within Custom Callback? - El Forum - 07-05-2010 [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'); 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'); Code: function new_email($str) Validate Email within Custom Callback? - El Forum - 07-05-2010 [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. EDIT(again): Fixed it! Thanks again! Here's the final function I ended up with: Code: function new_email($str) { |