PHP/String/Email Format
A regular expression for e-mail string checking
<?php
$email = "l@b.ca";
echo preg_match("/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+.(.[a-zA-Z0-9_-]+)+[a-zA-Z0-9_-]$/",$email); //Would return 1 (true).
echo "<br />";
$bademail = "l.ca";
echo preg_match("/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+.(.[a-zA-Z0-9_-]+)+[a-zA-Z0-9_-]$/",$bademail); //Would return 0 (false).
?>
Use regular to check email address
<?php
$regex = "/^[\w\d!#$%&"*+-\/=?^"{|}~]+(\.[\w\d!#$%&"*+-\/=?^"{|}~]+)*@([a-z\d][-a-z\d]*[a-z\d]\.)+[a-z][-a-z\d]*[a-z]$/";
$values = array(
"user@example.ru", // Valid
"u2@example.ru", // Valid
);
foreach ($values as $value) {
if (preg_match($regex, $value)) {
printf("Found valid address: %s\n", $value);
} else {
printf("INVALID address: %s\n", $value);
}
}
?>
Validate email
<?php
function validate_email($email) {
$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,6})$";
if (eregi($regexp, $email)) return 1;
else return 0;
}
if (isset($_POST["submit"])){
echo "Hi ".$_POST["name"]."!<br />";
if (validate_email($_POST["email"]))
echo "The address ".$_POST["email"]." is valid!";
else
echo "The address <strong>".$_POST["email"]."</strong> is invalid!";
}
?>
<form action="index.php" method="post">
Name:<input type="text" name="name" value="" />
Email Address:<input type="text" name="email" value="" />
<input type="submit" name = "submit" value="Go!" />
</form>