Jump to content

Email validation special script. Need help.


ricerocket

Recommended Posts

Hi, I've been searching around alot and just can't seem to find an email validation script that would work with my registration system, so if someone could help that would be great.

 

This is a section out of my registration script that runs the checks and it does it in a strange way so if you know how I would work in an email validation...

 

if(strlen($username)<3)
{
  print "Your username must be at least 4 characters long. Please go back and try again";
  print "</td></tr></table></td>";
  print "</table><img src='images/bottom.gif' /><br/><br/>";
}
else if($checkmail3>0)
{
  print "There is already another player registered with that email address or username.";
  print "</td></tr></table></td>";
  print "</table><img src='images/bottom.gif' /><br/><br/>";
}
else 
{
***On success insert details and send activation email***";
}

How would I make it so something like this:

 

 

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

 

Is used to display a certain message if the statement is false rather than:

 

if (check_email_address($email)) {
  echo $email . ' is a valid email address.';
} else {
  echo $email . ' is not a valid email address.';
}

 

I don't know if it's possible to do something like:

 

if (check_email_address($email))=FALSE {
  print "The email you entered is invalid";
}

 

But I'm just a newb so sry if that looks totally ridiculous, I'm learning.

This is part of my validation

//check the user input for email address
if(empty($email1)) { 
	$error['email1'] = true;
	$error['email2'] = true; 
	 $print_again = true; 
	$message.="<li>The <span><b>Email Address</b></span> field is empty</li>"; 
}
else if( mysql_num_rows(mysql_query("SELECT email FROM users WHERE email = '$email'")) ) {
	$error['email1'] = true; 
	 $print_again = true; 
	$message.="<li>Your email address has already been used by another member in our database. Please submit a different Email address!!</li>"; 
}
else if(!check_email_address($email1)) { 
	$error['email1'] = true; 
	 $print_again = true; 
	$message.="<li><span><b>Email address in invalid format</li>"; 
}
//compare the two given email addresses to confirm they match
if (strcmp( $email1,$email2 ) !=0){
	$error['email2'] = true; 
	 $print_again = true; 
	$message.="<li>The <span><b>Email addresses</b></span> didn't match</li>";
}

 

and the function to check the 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; 
} 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.