Jump to content

[SOLVED] Issue with Email Validation String


monkeytooth

Recommended Posts

Ok good with this particular type of thing I am not. So I could use any help possible.

Below I have a line of code that I am using to validate the format of an email address prior to storing it.. However I ran into a wall with it when I had someone come along with a hyphen in there email address, and they complained. The hyphen gave a "Bad". So far I got it validating emails like

 

[email protected]

[email protected]

[email protected]

 

those come back as "Good"

 

So what I am asking I suppose is how can I take the below line code and alter it so it validate emails like

[email protected]

[email protected]

 

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['T2'])) { echo "Good"; } else { echo "bad"; }

 

Unless someones got a better function concept then I am all ears to that as well.

eregi is being depreciated. I would avoid using it:

 

<?php
$pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])' .
'(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';

var_dump(preg_match ($pattern, "[email protected]"));
?>

 

Give that a try and see if that helps out at all.

 

EDIT:

If on a linux host I would look into also utilizing checkdnsrr to validate the host (everything after the @).

Your problem is probably that you have hyphens listed in your character classes (the stuff between the [..] brackets) but it's on the end.  Inside a character class, hyphens are special characters and denote a range from one thing to another.  The short, simple answer is that unless you list a hyphen as the first character in the character class, you might get unexpected results.  So for instance, in your first character class in your pattern:

 

[_a-z0-9-]

 

change it to

 

[-_a-z0-9]

 

but as premiso posted while I was typing, use preg_match instead, as the posix functions (eregxxx) are deprecated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.