Jump to content

preg_match


astralaaron
 Share

Recommended Posts

I am trying to add a preg_match to my registration to make sure they are entering a valid email address.anyone know what is wrong with this? if (!preg_match('{[0-9a-zA-Z]@[0-9a-zA-Z].[a-zA-Z]{2,3}}', "$email")){ header("location:http://localhost/vikingbjj/user/emailerror.php");}i also tried: if (!preg_match('{[0-9a-zA-Z]@[0-9a-zA-Z]\.[a-zA-Z]{2,3}}', "$email")){ header("location:http://localhost/vikingbjj/user/emailerror.php");}some help please!!EDIT** DOH! my variable was wrong, it is supossed to be $myemailbut still it will let me put in astralaaron@localhost with no .com at the endanyone know how to make it so it has to be an exact match to that?

Link to comment
Share on other sites

I dont think there is a dash between the A and Z and the lowercase ones to. I would recommend googling "php email validation" because there are better ones out there, you canuse the checkdnsrr function and such to make sure websites actually exist and are mailservers. You can also go to that function and look at the notes, there are a few functions that include blacklist checking.

Link to comment
Share on other sites

I dont think there is a dash between the A and Z and the lowercase ones to. I would recommend googling "php email validation" because there are better ones out there, you canuse the checkdnsrr function and such to make sure websites actually exist and are mailservers. You can also go to that function and look at the notes, there are a few functions that include blacklist checking.
I just watched a video tutorial on preg_match, I know [0-9a-zA-Z] is valid.it is working fine now, I just need to know how to make it so the email has to have the .com extention because it allows my astralaaron@localhost stillthanks for the reply though
Link to comment
Share on other sites

okay I copied this off the internet and it works perfectly:if(!eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,6}\$", "$myemail")){ header("location:http://localhost/vikingbjj/user/emailerror.php");}but i would like to know how to do it with preg_match too if anyone knows!

Link to comment
Share on other sites

Try this (you can't use { and } as "delimiter" [i think it's called]): /^[0-9a-zA-Z]*@[0-9a-zA-Z]*\.[a-zA-Z]{2,3}$/ That should workand remove the quots around $email...

Link to comment
Share on other sites

i put this:if(!preg_match '/^[0-9a-zA-Z]*@[0-9a-zA-Z]*\.[a-zA-Z]{2,3}$/', $myemail) { header("location:http://localhost/vikingbjj/user/emailerror.php"); }and I get this error:Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\wamp\www\vikingbjj\user\newidcheck.php on line 19

Link to comment
Share on other sites

And I just wanted to point out that you can use { } alsoit works like this too:if(!preg_match('{^[0-9a-zA-Z]*@[0-9a-zA-Z]*\.[a-zA-Z]{2,3}$}', $myemail)) { header("location:http://localhost/vikingbjj/user/emailerror.php"); }this guy talks about it in his video tutorials on phpvideotutorials.com (i just tried it also, works fine)

Link to comment
Share on other sites

That's not a good pattern to use to validate email. It will not match a domain with a subdomain, for example. It also leaves out several characters that are valid, such as underscore, period, hyphen, etc. It also leaves out 4-letter TLDs (.info, .aero, etc).http://www.google.com/search?client=opera&...-8&oe=utf-8

Link to comment
Share on other sites

That's not a good pattern to use to validate email. It will not match a domain with a subdomain, for example. It also leaves out several characters that are valid, such as underscore, period, hyphen, etc. It also leaves out 4-letter TLDs (.info, .aero, etc).http://www.google.com/search?client=opera&...-8&oe=utf-8
yeah I just realized this last night, I need to have the - _ . and 4 letters for the end also
Link to comment
Share on other sites

That first link that Google returns has a pretty complete pattern. They use eregi in that example, which is case insensitive, so if you wanted to use the pattern with preg_match you would need to include the "i" modifier on the pattern to make it case insensitive also.if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $email) {Also, here's a question for people. In that pattern, which is in double-quotes, they have several \. sequences. They want to send the slash as part of the pattern, so why don't they need to use \\.? Why does the above work?

Link to comment
Share on other sites

Hey I did that exact same search before I posted this thread, I tried that one from 'zend technologies'if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9- ]+)*(\.[a-z]{2,3})$", $email) { and for some reason it did not work. I had another one that was similar to that and it worked.but I wanted to know with preg_match (so thanks for your info on that!)and thanks for showing me the 'i' modifier that is very cool!

Link to comment
Share on other sites

Here is a better one that I use, it checks to make sure that the domain is, if it is a mail server, and if it is in the valid email format:

function check_email($email) {		if(preg_match('/^\w[-.\w]*@(\w[-._\w]*\.[a-zA-Z]{2,}.*)$/', $email, $matches))		{			if(function_exists('checkdnsrr'))			{				if(checkdnsrr($matches[1] . '.', 'MX')) return true;				if(checkdnsrr($matches[1] . '.', 'A')) return true;			}else{				if(!empty($hostName))				{					if( $recType == '' ) $recType = "MX";					exec("nslookup -type=$recType $hostName", $result);					foreach ($result as $line)					{						if(eregi("^$hostName",$line))						{							return true;						}					}					return false;				}				return false;			}		}		return false;	}

You can also use this function to check if the domain (in ip format) is on the blacklist as a spam server.

function blacklisted($ip) {	$dnsbl_lists = array("bl.spamcop.net", "list.dsbl.org", "sbl.spamhaus.org");	if ($ip && preg_match('/^([0-9]{1, 3})\.([0-9]{1, 3})\.([0-9]{1, 3})\.([0-9]{1, 3})/', $ip)) {		$reverse_ip = implode(".", array_reverse(explode(".", $ip)));		$on_win = substr(PHP_OS, 0, 3) == "WIN" ? 1 : 0;		foreach ($dnsbl_lists as $dnsbl_list){			if (function_exists("checkdnsrr")) {				if (checkdnsrr($reverse_ip . "." . $dnsbl_list . ".", "A")) {					return $reverse_ip . "." . $dnsbl_list;				}			} else if ($on_win == 1) {				$lookup = "";				@exec("nslookup -type=A " . $reverse_ip . "." . $dnsbl_list . ".", $lookup);				foreach ($lookup as $line) {					if (strstr($line, $dnsbl_list)) {						return $reverse_ip . "." . $dnsbl_list;					}				}			}		}	}	return false;}

Thats the most secure function(s) I have seen for email validation yet.P.S. To get the ip of a server/domain you can use the gethostbyname("http://www.domain.com") function.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...