Anyone have an Email String Validation Routine ?

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

Post Reply
HUWUWA
Forum Commoner
Posts: 35
Joined: Sat Aug 17, 2002 7:24 pm
Location: Long Island, NY

Anyone have an Email String Validation Routine ?

Post by HUWUWA »

Hi guys, I'm just wondering if anyone has a code snippet for validating an Email string. You know, checking for the '@' symbol, and a '.' and making sure the '@' symbol isn't first and the whole string is a certain length, etc.

I guess I can program it myself but if there is a solid one already I may as well use it 8)

Thanks.
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Code: Select all

<?
// check if email address is valid
function isEmailInvalid($val)
&#123;
	// regex for email validation
	$pattern =
"/^(&#1111;a-zA-Z0-9])+(&#1111;\.a-zA-Z0-9_-])*@(&#1111;a-zA-Z0-9_-])+(\.&#1111;a-zA-Z0-9_-]+)+/";

		// match?
		if(preg_match($pattern, $val))
		&#123;
		return 0;
		&#125;
		else
		&#123;
		return 1;
		&#125;
&#125;
?>
HUWUWA
Forum Commoner
Posts: 35
Joined: Sat Aug 17, 2002 7:24 pm
Location: Long Island, NY

Post by HUWUWA »

Hell Yeah Pete,

It works great, thanks man. Now I just wish I understood it :(

I'll use it as is though. Thanks again.
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Here's a good tutorial at PHP Builder

It's straight forward...it deals with ereg, but you can modify for preg no problem ( preg is faster ).

Glad to help,

Regards,

PAS
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

Sorry to be extremely picky about this, but I must: what the HELL is the point of e-mail validation!? I mean really...unless there was some way to make sure that the actual person OWNED the e-mail adress they put in...it is COMPLETELY pointless. So say i put in the letter 'x'....your validation script wastes SPACE and TIME, checking for certain things. So it tells me i need an '@' symbol with stuff before and after it, etc. So I go back and write 'bob@aol.com'...CONGRATULATIONS, my e-mail address was incorrect, yet it was accepted anyway...thus proving that your validation is pointless!

I somebody wants to BS an e-mail address, the only thing u are doing is making it take 5 seconds longer.......

So, screw the idea :wink:

later on, -Brian
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

brian, I think its to save people from mispelling their email address

I had many entries where people forgot the @ symbol or put me@aol and forgot the.com or made some other mistake in their address. if someone wants to put a bad addy in, they always can but at least make them put it in the right format :)
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Site registration & confirmations...

confirmation of registration on a website by returning the "Welcome" email sent to the validated email address.
HUWUWA
Forum Commoner
Posts: 35
Joined: Sat Aug 17, 2002 7:24 pm
Location: Long Island, NY

Post by HUWUWA »

Of course it cannot totally validate a real email address but it does make the form look professional. At least you won't have idiots entering simply 'Joe' as their email address.

In my case only people trying to contact me will use the form so I don't mind the test.

Can you imagine how unprofessional your site would look if a guy entered 'Death' as their email address and the mail() function actually went through ?
Post Reply