jdowen2211 Posted July 31, 2011 Share Posted July 31, 2011 I want to add an email option to a HTML that parses with PHP and submits data to a MySQL database but I'm not too sure are submitting email. I have the email field already created in the structure of the database but when the form is submitted, I want to make sure the email is valid. I use this for other $row's: $firstname = preg_replace('#[^A-Za-z]#i', '', $_POST['firstname']); // filter everything but desired characters So what preg_replace would I use for email? Quote Link to comment https://forums.phpfreaks.com/topic/243378-preg_replace-for-submitting-email-into-mysql-database/ Share on other sites More sharing options...
IrOnMaSk Posted August 3, 2011 Share Posted August 3, 2011 to validate that email address is real, use this simple function function myCheckDNSRR($hostName, $recType = '') <BR> { <BR> if(!empty($hostName)) { <BR> if( $recType == '' ) $recType = "MX"; <BR> exec("nslookup -type=$recType $hostName", $result); <BR> // check each line to find the one that starts with the host <BR> // name. If it exists then the function succeeded. <BR> foreach ($result as $line) { <BR> if(eregi("^$hostName",$line)) { <BR> return true; <BR> } <BR> } <BR> // otherwise there was no mail handler for the domain <BR> return false; <BR> } <BR> return false; <BR> } Don't get it? go here http://www.sitepoint.com/users-email-address-php/ Quote Link to comment https://forums.phpfreaks.com/topic/243378-preg_replace-for-submitting-email-into-mysql-database/#findComment-1251506 Share on other sites More sharing options...
AyKay47 Posted August 3, 2011 Share Posted August 3, 2011 well you wouldn't really want to use preg_replace on an email...as it can strip characters that will make the email useless...however if a user includes unwanted characters invalid to an email address, the email address is obviously invalid anyway...heres a pattern to be used with preg_match $pattern = '~[a-zA-Z0-9-_.]+@[a-zA-Z]+\.[a-z]{2,4}~i'; $subject = 'example.test@example.com'; if(preg_match($pattern,$subject)){ //if TRUE, a match has been found print "valid email"; }else{ print "invalid email"; } now there are a couple ways to limit the extension and host, however this is a simple and fairly secure pattern to use.. Quote Link to comment https://forums.phpfreaks.com/topic/243378-preg_replace-for-submitting-email-into-mysql-database/#findComment-1251527 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.