CodeIgniter Forums
Email validation - 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: Email validation (/showthread.php?tid=36782)



Email validation - El Forum - 12-14-2010

[eluser]alexaaaaaaaaaa[/eluser]
Hi i have a problem with the email validator, it seems that if i insert something like
[email protected] it's working and it's passing the email validator any extension to the email validator or how to fix this issue?
Thanks


Email validation - El Forum - 12-15-2010

[eluser]Madmartigan1[/eluser]
I think [email protected] is technically valid...?

Code:
/**
     * Valid Email
     *
     * @access    public
     * @param    string
     * @return    bool
     */
    function valid_email($str)
    {
        return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
    }

You can create MY_Form_Validation.php and override the function there with your own. See the user guide section about extending libraries and have a look at system/libraries/Form_Validation.php


Email validation - El Forum - 12-15-2010

[eluser]smilie[/eluser]
Yes, by judging that preg match, after @ sign it expects minimum of 1 letter / number.

I am using following script for domain and e-mail checks:
http://code.google.com/p/blogchuck/source/browse/#svn/trunk

You have domains / email and phones scripts there.
Simply add them as a helper, load it and call function (providing e-mail address).

It uses IANA list of domains to check validity of extension and it can even 'call' the mail server to check that e-mail address really exists on that server - which makes it almost 100% safe check for the mail.

Cheers,
Smilie


Email validation - El Forum - 12-15-2010

[eluser]LuckyFella73[/eluser]
@smilie: thanks for sharing the validation link - it looks quite interesting!

You said "just load as a helper" -> did you copy the "domains" class to the
"emails" class to get it working? The class "emails" extends the class "domains"
so I wonder what would be the best to do.


Email validation - El Forum - 12-15-2010

[eluser]smilie[/eluser]
Hi,

That could probably work as well, however I have chosen for the following approach (due to the fact that I have multiple websites under 1 CI installation):

I have placed domains.php as domain_helper.php in the core/helpers (not application);
Then, in each application/config/autoload.php I do:

Code:
$autoload['helper'] = array('html','url','language','domain');

Whereby 'domain' loads the before mentioned helper.

Then, in the controllers I do:

Code:
$domains = new Domains;
if(!$domains->validateDomain($this->input->post('servername'), false))
{
   # Domain is NOT valid
}
else
{
   # Domain is VALID
}
Same goes for email class and phone class (tho' I do not use phone class).

Do note, that you have to download the IANA domain extension list and edit config in the domains helper so it can be 'found' by the helper. I have also placed in the cron of the server to get once a week 'newest' IANA list (although, it does not change weekly - it can't hurt) :-)

Have fun!

Cheers,
Smilie


Email validation - El Forum - 12-15-2010

[eluser]LuckyFella73[/eluser]
Thanks for explaining your setup! I'll try to implement that classes
in my next project where that kind of validation is needed.


Email validation - El Forum - 12-15-2010

[eluser]alexaaaaaaaaaa[/eluser]
Hi, thanks for the link, i have created a file named validation_helper.php and i've copy pasted the code from the link there.
Now how can i use it?
Please help me.
Many thanks