energysuperstore09 Posted March 31, 2009 Share Posted March 31, 2009 I have a login page where they have to regsiter for a login and password, but I want to make sure they enter a valid email address. Does anybody know how I would do that? Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/ Share on other sites More sharing options...
revraz Posted March 31, 2009 Share Posted March 31, 2009 And what do you want to use to determine the address is valid? You can use RegEx for the format, or you can make them reply to a email with a code. Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797779 Share on other sites More sharing options...
energysuperstore09 Posted March 31, 2009 Author Share Posted March 31, 2009 what would the process be to have them reply to an email with a code. Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797780 Share on other sites More sharing options...
lonewolf217 Posted March 31, 2009 Share Posted March 31, 2009 check this out to programatically make sure the email is valid http://www.totallyphp.co.uk/code/validate_an_email_address_using_regular_expressions.htm Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797781 Share on other sites More sharing options...
energysuperstore09 Posted March 31, 2009 Author Share Posted March 31, 2009 So i would use this? <?php $email = "someone@example.com"; if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { echo "Valid email address."; } else { echo "Invalid email address."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797785 Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 I you want to make sure the email address actually even looks like an email address, Try this tutorial, its came in very handy to me when writing regex. Check the Matching Patterns section for the email validation code. If you want to validate their email address, once they enter the address you'd create a random string (something like md5(rand()."salt string")) and save the string, and there email address in a table with 2 other fields, a unique id and a field determining whether or not the email has been validated. Use the random string in a url like www.domain.com/validate.php?key=flhdi7fgi3hkjnfp92u2hl9f3 and email that to them... they click on the link and when they get there the table is updated to say that that email address has been validated. Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797787 Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 So i would use this? <?php $email = "someone@example.com"; if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { echo "Valid email address."; } else { echo "Invalid email address."; } ?> Looks like it would work Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797789 Share on other sites More sharing options...
energysuperstore09 Posted March 31, 2009 Author Share Posted March 31, 2009 so i used the code that i posted and i put a email address that didn't even have the @ symbol in it and it echoed that it was a valid email address. any advise as I am new to the php thing Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797795 Share on other sites More sharing options...
energysuperstore09 Posted March 31, 2009 Author Share Posted March 31, 2009 I want to discourge people from using fictisous emails just to get login info Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797798 Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 use the one I suggested lol, by just reading it I don't see whats wrong with it, but I'm not the greatest at regexp Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797800 Share on other sites More sharing options...
lonewolf217 Posted March 31, 2009 Share Posted March 31, 2009 its entirely possible there is something wrong with the regex. I didn't write it, i just did a google search and picked a result. You can do your own search, pick a regex function to try and see if it works. There's tons of them out there edit: i tested it and the regex works just fine "someone@example.com" validates "someoneexample.com" does not validate Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797803 Share on other sites More sharing options...
premiso Posted March 31, 2009 Share Posted March 31, 2009 Untested, but I would suggest against eregi as it is being depreciated. <?php $email = "someone@test.com"; $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'; $valid = preg_match($pattern, $email); if ($valid == 1) { echo 'Valid email'; }else { echo 'InValid email'; } ?> Pulled from preg_match user comments. That will only solve the issue of a valid email address. To make sure the domain has an mx record you can use checkdnsrr to validate that portion (the host part): <?php function validateEmail($email) { list(,$host) = explode("@", $email); if (empty($host)) return false; if (!checkdnsrr($host, "MX")) return false; $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'; return preg_match($pattern, $email); } if (validateEmail('example@sometest.com')) { echo 'Email valid.'; }else { echo 'Email not valid.'; } ?> The code above is untested, so may have issues with syntax, fix those and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797818 Share on other sites More sharing options...
revraz Posted March 31, 2009 Share Posted March 31, 2009 There are many examples in the RegEx forum here. http://www.phpfreaks.com/forums/index.php/board,43.0.html Quote Link to comment https://forums.phpfreaks.com/topic/151925-php-valid-email/#findComment-797825 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.