Jump to content

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

 

mynameis@somedomain.com

my.nameis@some.domain.com

my_nameis@somedomain.com

 

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

my-nameis@somedomain.com

mynameis@some-domain.com

 

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-address_to.validate@host.tld"));
?>

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.