PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
icesolid
Forum Regular
Posts: 502 Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY
Post
by icesolid » Mon Oct 09, 2006 12:18 pm
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.
}
?>
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Mon Oct 09, 2006 12:52 pm
Code: Select all
preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/iu', trim($value));Google for it as well
icesolid
Forum Regular
Posts: 502 Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY
Post
by icesolid » Mon Oct 09, 2006 2:24 pm
If you don't mind, explain to me how that algorithm checks a valid e-mail address.
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Mon Oct 09, 2006 2:27 pm
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.
icesolid
Forum Regular
Posts: 502 Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY
Post
by icesolid » Mon Oct 09, 2006 2:43 pm
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.
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Mon Oct 09, 2006 2:58 pm
OK, well if you care now... you've got two new ways to validate.