Verify Email Field


#1

Hey,

What I’m trying to do is verify an email field in a form. I need the php to check and make sure that the data entered into the email field is of valid format (something@something.com). I’m looking for the code to run that check on the email field.

Any help would be great,

Nick


#2

Use the preg_match function:
http://ca2.php.net/manual/en/function.preg-match.php

The regex for an e-mail is “^[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$”


#3

Tarential, i’ve pulled this off of someone elses script… any reason for the excess statements in it? I havn’t a clue what the ending is talking about… the beginning yes but about 3/4 way through I loose it…

[php]
^[_a-z0-9-]+(.[_a-z0-9-]+)@(0-9a-z.)+[a-z]{2}([zmuvtg]|fo|me)?$
[/php]

that has been what i’ve used for email validatno for a year or so now, and no problems as far as i can tell…


#4

Drewbee: That regex is SEVERLY lacking. Unless you use the /i in which case it is only partially lacking, and somewhat redundant. Use mine. Its the one I got off the PHP site.


#5

Ok.

What is that going on at tne end…
[php]
zmuvtg]|fo|me
[/php]

?


#6

It is verifying the “.com” or “.net” or whatever. It works, but not in all cases. New domain types and such are added all the time.


#7

getmxrr and checkdnsrr allow you to check for dns and MX entries. Zend has a pretty nice verification script:
Zend script


#8

thats one seriously wild email validation script :)


#9

Yep. Thats what I’d call an e-mail verification script on steroids.


#10

This will check the domain to make sure it is a valid domain using checkdnsrr. It doesn’t work on php for win32, but it works great on *nix.
Test it out. Just replace the “$your_email_value” with your email field value.

list($userName, $mailDomain) = split("@", $your_email_value); if (checkdnsrr($mailDomain, "MX")) { // this is a valid email domain! } else { // this email domain doesn't exist! }


#11

Actually, since email addresses cam come in the form of

Tahongawaka <"spam trap"@rallycentral.us (this address is a spam trap)>, the regex is a bit more complex than you think. Addresses may have comments, and they may be nested. They may also have a “non address” part, quotes, sub-domains, ascii characters, and more! (See RFC 822)

When it comes down to it, there is [i]absolutely no way[/i] to ensure a particular address actually reaches someone. Period.

The regex for an email address with only one set of nested comments is closer to

[code][40t](?:([^\x80-xffn15()](?:(?:[^x80-xff]|([^\x80-
xffn15()](?:[^x80-xff][^\x80-xffn15()])))[^\x80-xf
fn15()]
))[40t])(?:(?:[^(40)<>@,;:".[]00-37x80-x
ff]+(?![^(40)<>@,;:".[]00-37x80-xff])|"[^\x80-xffn15
"]
(?:[^x80-xff][^\x80-xffn15"])")[40t](?:([^\x80-
xffn15()]
(?:(?:[^x80-xff]|([^\x80-xffn15()](?:[^x80
-xff][^\x80-xffn15()]
)))[^\x80-xffn15()]))[40t]
)(?:.[40t](?:([^\x80-xffn15()](?:(?:[^x80-xff]|([^
\x80-xffn15()]
(?:[^x80-xff][^\x80-xffn15()])))[^
x80-xffn15()]))[40t])(?:[^(40)<>@,;:".[]00-37x8
0-xff]+(?![^(40)<>@,;:".[]00-37x80-xff])|"[^\x80-xffn
15"](?:[^x80-xff][^\x80-xffn15"])")[40t](?:([^\x
80-xffn15()](?:(?:[^x80-xff]|([^\x80-xffn15()](?:[^
x80-xff][^\x80-xffn15()])))[^\x80-xffn15()]))[40
t]))@[40t](?:([^\x80-xffn15()](?:(?:[^x80-xff]|([
^\x80-xffn15()]
(?:[^x80-xff][^\x80-xffn15()])))[^
\x80-xffn15()]))[40t])(?:[^(40)<>@,;:".[]00-37
x80-xff]+(?![^(40)<>@,;:".[]00-37x80-xff])|[(?:[^\x80-
xffn15[]]|[^x80-xff])])[40t](?:([^\x80-xffn15()
](?:(?:[^x80-xff]|([^\x80-xffn15()](?:[^x80-xff][^
x80-xffn15()])))[^\x80-xffn15()]))[40t])(?:.[4
0t](?:([^\x80-xffn15()](?:(?:[^x80-xff]|([^\x80-xff
n15()](?:[^x80-xff][^\x80-xffn15()])))[^\x80-xffn
015()]
))[40t])(?:[^(40)<>@,;:".[]00-37x80-xff]+(?!
[^(40)<>@,;:".[]00-37x80-xff])|[(?:[^\x80-xffn15[
]]|[^x80-xff])
])[40t](?:([^\x80-xffn15()](?:(?:[^
x80-xff]|([^\x80-xffn15()](?:[^x80-xff][^\x80-xffn1
5()]
)))[^\x80-xffn15()]))[40t]))|(?:[^(40)<>@,;:".
[]00-37x80-xff]+(?![^(40)<>@,;:".[]00-37x80-xff]
)|"[^\x80-xffn15"](?:[^x80-xff][^\x80-xffn15"])")[^
()<>@,;:".[]x80-xff00-1012-37]
(?:(?:([^\x80-xffn
15()](?:(?:[^x80-xff]|([^\x80-xffn15()](?:[^x80-xff][
^\x80-xffn15()])))[^\x80-xffn15()]))|"[^\x80-xff
n15"](?:[^x80-xff][^\x80-xffn15"])")[^()<>@,;:".[]
x80-xff00-1012-37]
)<[40t](?:([^\x80-xffn15()](?
:(?:[^x80-xff]|([^\x80-xffn15()]
(?:[^x80-xff][^\x80-
xffn15()])))[^\x80-xffn15()]))[40t])(?:@[40t]*
(?:([^\x80-xffn15()](?:(?:[^x80-xff]|([^\x80-xffn15
()]
(?:[^x80-xff][^\x80-xffn15()])))[^\x80-xffn15()
]))[40t])(?:[^(40)<>@,;:".[]00-37x80-xff]+(?![^(
40)<>@,;:".[]00-37x80-xff])|[(?:[^\x80-xffn15[]]|
[^x80-xff])])[40t](?:([^\x80-xffn15()](?:(?:[^x80-
xff]|([^\x80-xffn15()]
(?:[^x80-xff][^\x80-xffn15()]*
)))[^\x80-xffn15()]))[40t])(?:.[40t](?:([^\x80
-xffn15()](?:(?:[^x80-xff]|([^\x80-xffn15()](?:[^x
80-xff][^\x80-xffn15()])))[^\x80-xffn15()]))[40t
])(?:[^(40)<>@,;:".[]00-37x80-xff]+(?![^(40)<>@,;:".
[]00-37x80-xff])|[(?:[^\x80-xffn15[]]|[^x80-xff])
])[40t](?:([^\x80-xffn15()](?:(?:[^x80-xff]|([^\x
80-xffn15()]
(?:[^x80-xff][^\x80-xffn15()])))[^\x80
-xffn15()]))[40t]))(?:,[40t](?:([^\x80-xffn15(
)](?:(?:[^x80-xff]|([^\x80-xffn15()](?:[^x80-xff][^
x80-xffn15()])))[^\x80-xffn15()]))[40t])@[40t
](?:([^\x80-xffn15()](?:(?:[^x80-xff]|([^\x80-xffn
15()](?:[^x80-xff][^\x80-xffn15()])))[^\x80-xffn15
()]
))[40t])(?:[^(40)<>@,;:".[]00-37x80-xff]+(?![^(
40)<>@,;:".[]00-37x80-xff])|[(?:[^\x80-xffn15[]]|
[^x80-xff])
])[40t](?:([^\x80-xffn15()](?:(?:[^x80
-xff]|([^\x80-xffn15()](?:[^x80-xff][^\x80-xffn15()
]
)))[^\x80-xffn15()]))[40t])(?:.[40t](?:([^\x
80-xffn15()](?:(?:[^x80-xff]|([^\x80-xffn15()](?:[^
x80-xff][^\x80-xffn15()])))[^\x80-xffn15()]))[40
t])(?:[^(40)<>@,;:".[]00-37x80-xff]+(?![^(40)<>@,;:".
[]00-37x80-xff])|[(?:[^\x80-xffn15[]]|[^x80-xff
])])[40t](?:([^\x80-xffn15()](?:(?:[^x80-xff]|([^
x80-xffn15()]
(?:[^x80-xff][^\x80-xffn15()])))[^\x
80-xffn15()]))[40t]))):[40t](?:([^\x80-xffn15
()]
(?:(?:[^x80-xff]|([^\x80-xffn15()](?:[^x80-xff][^
\x80-xffn15()]
)))[^\x80-xffn15()]))[40t]))?(?:[^
(40)<>@,;:".[]00-37x80-xff]+(?![^(40)<>@,;:".[]00-
37x80-xff])|"[^\x80-xffn15"]
(?:[^x80-xff][^\x80-xff
n15"])")[40t](?:([^\x80-xffn15()](?:(?:[^x80-xff]|
([^\x80-xffn15()](?:[^x80-xff][^\x80-xffn15()])))
[^\x80-xffn15()]
))[40t])(?:.[40t](?:([^\x80-xff
n15()](?:(?:[^x80-xff]|([^\x80-xffn15()](?:[^x80-x
ff][^\x80-xffn15()])))[^\x80-xffn15()]))[40t])(
?:[^(40)<>@,;:".[]00-37x80-xff]+(?![^(40)<>@,;:".[]
000-37x80-xff])|"[^\x80-xffn15"](?:[^x80-xff][^\x80-
xffn15"]
)")[40t](?:([^\x80-xffn15()](?:(?:[^x80-x
ff]|([^\x80-xffn15()]
(?:[^x80-xff][^\x80-xffn15()])
))[^\x80-xffn15()])
)[40t]))@[40t](?:([^\x80-x
ffn15()](?:(?:[^x80-xff]|([^\x80-xffn15()](?:[^x80-
xff][^\x80-xffn15()])))[^\x80-xffn15()]))[40t])
(?:[^(40)<>@,;:".[]00-37x80-xff]+(?![^(40)<>@,;:".[
]00-37x80-xff])|[(?:[^\x80-xffn15[]]|[^x80-xff])
]
)[40t]
(?:([^\x80-xffn15()](?:(?:[^x80-xff]|([^\x80-
xffn15()]
(?:[^x80-xff][^\x80-xffn15()])))[^\x80-x
ffn15()]))[40t])(?:.[40t](?:([^\x80-xffn15()](
?:(?:[^x80-xff]|([^\x80-xffn15()](?:[^x80-xff][^\x80
-xffn15()]
)))[^\x80-xffn15()]))[40t])*(?:[^(40)<

@,;:".[]00-37x80-xff]+(?![^(40)<>@,;:".[]00-37x8
0-xff])|[(?:[^\x80-xffn15[]]|[^x80-xff])])[40t](?:
([^\x80-xffn15()](?:(?:[^x80-xff]|([^\x80-xffn15()]
(?:[^x80-xff][^\x80-xffn15()])
))[^\x80-xffn15()])
)[40t])
)*>)
[/code]


#12

Holy :o


#13

I’m glad at least someone knows how to do regular expressions :) heaven knows I can’t!


#14

i use this all the time in my scripts, never failed once…

<?PHP

	function EmailIsValid($email) {
    
	list($local, $domain) = explode("@", $email);
	$pattern_local = '^([0-9a-z]*([-|_]?[0-9a-z]+)*)(([-|_]?).([-|_]?)[0-9a-z]*([-|_]?[0-9a-z]+)+)*([-|_]?)$';
	$pattern_domain = '^([0-9a-z]+([-]?[0-9a-z]+)*)(([-]?).([-]?)[0-9a-z]*([-]?[0-9a-z]+)+)*.[a-z]{2,4}$';
	$match_local = eregi($pattern_local, $local);
	$match_domain = eregi($pattern_domain, $domain);
    	
    		if ($match_local && $match_domain) {
    			return 1;
    		} else {
    			return 0;
    		}
	}

?>