...

View Full Version : Email validation



Schteeve
11-08-2007, 05:11 PM
Hi,

Am trying to integrate some code i found at ilovejackdaniels.com (http://http://www.ilovejackdaniels.com/php/email-address-validation/) into my form processing script.
Can anyone point out where I'm going wrong. :confused:


<?php
$email=$_POST['visitor_email'];

// Validate 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;
}

if (check_email_address($email)) {
}
else{
$error.="Please, go back and check your <strong>Email Address</strong><br><br><a href=\"javascript: history.go(-1)\">< Back</a>\n";//----> ERROR if invalid
}

//-------->ERROR FREE??
if($error==""){

?>

Any help would be much appreciated!

aedrin
11-08-2007, 06:47 PM
Can anyone point out where I'm going wrong.

Can you? We need to know what the issue is before we can help.

Schteeve
11-09-2007, 10:10 AM
Sorry having trouble explaining myself :confused:

I need the code to display an error message if the email address is invalid and stop the process.

i believe it's this final else statement that's the problem, the error message does not display and the process continues.

else{
$error.="Please, go back and check your <strong>Email Address</strong><br><br><a href=\"javascript: history.go(-1)\">< Back</a>\n";//----> ERROR if invalid
}

Sorry if this is all seems a bit vague...still quite a noob at this.

nikos101
11-09-2007, 10:13 AM
So you input an invalid email and nothing happens?

Schteeve
11-09-2007, 11:02 AM
It's ok, have fixed the problem...sorry to have bothered you.

I Was making it much more complicated than it needed to be, as is always the way the answer was right in front of me all along!

Thanks again!!

nikos101
11-09-2007, 11:05 AM
Please post your valid code so we can learn from it :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum