mattyd247
asked on
php email validation problem
Hi,
I'm having a few problems validating email addresses using the following function: (see attched code snippet)
It won't allow email adresses with hyphens ... eg:
'matt.down@domain.com' is fine .. but... matt-stephen.down@domain.c om' is rejected.
I'm no expert on regular expressions so any help would be really appreciated.
I'm having a few problems validating email addresses using the following function: (see attched code snippet)
It won't allow email adresses with hyphens ... eg:
'matt.down@domain.com' is fine .. but... matt-stephen.down@domain.c
I'm no expert on regular expressions so any help would be really appreciated.
// check for a valid email address
function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
obviously handy as it checks the mx record of the domain as well
ASKER
Thanks for the suggestion but I'm getting this now:
Call to undefined function getmxrr()
Ideally I'd like to just work out what is missing from my own regular expression so that it accepts hyphens in the email address.
Call to undefined function getmxrr()
Ideally I'd like to just work out what is missing from my own regular expression so that it accepts hyphens in the email address.
maybe im just tired but Ive run your code with a email with a '-' char in it. Seems to work
<?php
// check for a valid email address
function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
$email="matt-stephen.down@domain.com";
$test=check_email_address($email);
if($test==1)
{
print "this email is valid";
}
else
{
print "this email is not valid";
}
?>