RegEx Email Address Verification
Very simple regular expression email address verification function
I built this to deal with the structure of an email address, not whether or
not it had a valid TLD on the end of it or whatever. This simply checks to make
sure that the address has valid email address characters to the left of an @
symbol and a valid domain name to the right.
PHP Source Code
<?php
// isValidEmail( [string] )
// returns 0 if email address is invalid
// returns 1 if email address is good
function isValidEmail
( $e ) {
return preg_match( '/^[_a-z0-9-][^\(\)\<\>\@\,\;\:\\\"\[\] ]*\@([a-z0-9\-]+\.)+[a-z]{2,4}$/i',
$e );
}
$msg =
'';
if( !
empty( $_POST['address'] ) ) {
if( isValidEmail
( $_POST['address'] ) ) {
$msg =
'<p style="color:#009900">Good email address!</p>';
} else {
$msg =
'<p style="color:#990000">Bad email address.</p>';
}
}
?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns=
"http://www.w3.org/1999/xhtml" version=
"-//W3C//DTD XHTML 1.1//EN" xml:lang=
"en">
<head>
<meta http-equiv=
"Content-Type" content=
"application/xhtml+xml; charset=utf-8" />
<link href=
"/includes/css/ozStyle.css.php" type=
"text/css" rel=
"stylesheet" />
<title>Email Validation Demo</title>
</head>
<body style=
"margin:10px">
<h1>Email Validation</h1>
<?= !
empty( $msg ) ?
$msg :
'' ?>
<form method=
"post" style=
"margin: 0px; padding: 0px;">
<p>Email address:<br />
<input name=
"address" type=
"text" value=
"<?=$_POST['address']?>" size=
"60" maxlength=
"255"></p>
<p><input type=
"submit" value=
"submit" /></p>
</form>
</body>
</html>