monkeytooth Posted March 11, 2009 Share Posted March 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148969-solved-issue-with-email-validation-string/ Share on other sites More sharing options...
premiso Posted March 11, 2009 Share Posted March 11, 2009 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 @). Quote Link to comment https://forums.phpfreaks.com/topic/148969-solved-issue-with-email-validation-string/#findComment-782194 Share on other sites More sharing options...
.josh Posted March 11, 2009 Share Posted March 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148969-solved-issue-with-email-validation-string/#findComment-782199 Share on other sites More sharing options...
monkeytooth Posted March 11, 2009 Author Share Posted March 11, 2009 Well thank you guys, specially premiso that helped alot, was able to turn that into a nice little function. Quote Link to comment https://forums.phpfreaks.com/topic/148969-solved-issue-with-email-validation-string/#findComment-782237 Share on other sites More sharing options...
premiso Posted March 11, 2009 Share Posted March 11, 2009 Well thank you guys, specially premiso that helped alot, was able to turn that into a nice little function. Honestly the credit goes to the preg_match user comments section. I simply copy and pasted. Quote Link to comment https://forums.phpfreaks.com/topic/148969-solved-issue-with-email-validation-string/#findComment-782241 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.