Page 1 of 1

E-mail Validation

Posted: Mon Oct 09, 2006 12:18 pm
by icesolid
I was wondering if there is any better way of validating an e-mail address. Currently this is how I do so:

Code: Select all

<?php
if(!eregi("@", $_POST["email"]) || !eregi("\.", $_POST["email"])) {
    // This is a bad e-mail.
} else {
    // This is a good e-mail.
}
?>

Posted: Mon Oct 09, 2006 12:52 pm
by Ollie Saunders

Code: Select all

preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/iu', trim($value));
Google for it as well

Posted: Mon Oct 09, 2006 2:24 pm
by icesolid
If you don't mind, explain to me how that algorithm checks a valid e-mail address.

Posted: Mon Oct 09, 2006 2:27 pm
by Luke
It's a Regular Expression. Also known as regex. There is a whole forum dedicated to it on this site.

Oh yea... and just FYI, to search for a character in a string like you were doing before, just use strpos...

Code: Select all

<?php
if(!strpos($_POST["email"], "@") || !strpos($_POST["email"], "\.")) {
    // This is a bad e-mail.
} else {
    // This is a good e-mail.
}
?>
Oh yea... and so as to not be shot in the head by feyd, there's this: http://svn.gna.org/viewcvs/blacknova/tr ... iew=markup
It validates RFC compliant email addresses.

Posted: Mon Oct 09, 2006 2:43 pm
by icesolid
Thank you for the help.

No I do not want to be shot in the head by fyed, and this post was simply for information or opinions.

E-mail validation was never really important to be because if someone atleast types in a @ symbol and a .domain in I could really care less. If they wanted to put in a fake e-mail I never cared because I was just using it to keep records, hardly ever using the e-mail to contact the person.

Posted: Mon Oct 09, 2006 2:58 pm
by Luke
OK, well if you care now... you've got two new ways to validate.