Jump to content


Photo

Checking for valid email address format


  • Please log in to reply
12 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 13 August 2006 - 02:28 PM

I have tried a few example on checking an email address format but I can't get any to work unless I am doing it wrong.

I tried a couple of examples and some of them reported errors so am looking for advice

Here is my code and i've taken out the code that causes the errors
<?
session_start();
session_register("session");

if(!isset($session['userid'])){
echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>";
exit;
}

include 'includes/connection.php';

$query = "SELECT * FROM users WHERE userid = '".$_SESSION['userid']."'";

$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$id = $row['userid'];
$firstname = $row['first_name'];
$lastname = $row['last_name'];
$emailaddress = $row['email_address'];
$db_password = $row['password'];
}

// This is displayed if all the fields are not filled in
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";

// Convert to simple variables 
$first_name = stripslashes($_POST['first_name']); 
$last_name = stripslashes($_POST['last_name']);
$email_address = stripslashes($_POST['email_address']);

if (!isset($_POST['first_name'])) {
?>
<h2>Update personal details!</h2>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

	<p class="style3"><label for="first_name" style="width:8em">First Name:</label>
        <input type="text" title="Please enter your first name" name="first_name" size="30" value="<? echo $firstname; ?>"/></p>

        <p class="style3"><label for=last_name" style="width:8em">Second Name:</label>
        <input type="text" title="Please enter your last name" name="last_name" size="30" value="<? echo $lastname; ?>"/></p>

        <p class="style3"><label for="email_address" style="width:8em">Email address:</label>
        <input type="text" title="Enter your email address" name="email_address" size="30" value="<? echo $emailaddress; ?>"/></p>		

	<p class="style3"><label title="Login">
    <input type="submit" value="Update" style="margin-left:97px" class="submit-button"/></label></p>
</form>
<?php
}
elseif (empty($first_name) || empty($last_name) || empty($email_address))  {

    echo $empty_fields_message;

}

else {

if (strcmp( $first_name,$firstname ) !=0 || strcmp( $last_name,$lastname ) !=0 || strcmp( $email_address,$emailaddress ) !=0){
	if(mysql_query("update users SET first_name='$first_name', last_name='$last_name', email_address='$email_address' WHERE userid='$id'")){
	if (strcmp( $email_address,$emailaddress ) !=0) {
I want to put the validation after the end of this code.

In one exapmple i looked at from http://www.spoono.co...orial.php?id=41 i got some errors about fsocks.

I am looking for a tutorial or example that does actually work and check for email text box $email_address
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 whare

whare
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 13 August 2006 - 02:32 PM

I not sure if this is what you want but i hope it is its the members system by phpfreak using email verification for account activation

http://www.phpfreaks...orials/40/0.php

Hope it helps anyway

#3 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 13 August 2006 - 05:38 PM

I have used this for my members system but i need to prevent a user entering an invalid email address.

Currently there is nothing stopping the user entering "lgkhgsdhgjsfdhi" as their email address.  I want something that looks for valid characters such as . and @
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#4 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 13 August 2006 - 05:48 PM

strchr(string,search)

string refers to teh string you'll be searching, in this case the e-mail address and the character you want to search for.

strchr($email, "@"); would search $email for @, if @ is nto in email, teh result will be false

<?php
if (strchr($email, "@") and strchr($email, ".")) //contains "@" and "."
{
   email is valid
}
else
{
   email is invalid
}
?>


#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 13 August 2006 - 05:52 PM

@ScottRiley- Is the following string a valid email- "@$#^^.."?

Here is what I use:
<?php
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})%body%quot;, $email)){
echo "email invalid!";
}else{
echo "email valid!";
}
?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#6 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 13 August 2006 - 05:55 PM

Thanks for pointing that out, I'm still a n00b when it comes to PHP  ::), and I haven't came across the 'eregi' statement yet, i checked the manual and it seems very useful, thanks.

#7 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 13 August 2006 - 09:37 PM

I got some code from http://www.spoono.co...orial.php?id=41 and now I get this error.

Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/adrock/public_html/jack/email.php on line 17

Warning: fsockopen(): unable to connect to :25 in /home/adrock/public_html/jack/email.php on line 17
E-mail entered is not valid.

I just wanted to make sure the code worked so i knocked up a quick form

<?
function checkEmail($email) 
{
   if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 
   {
      return FALSE;
   }

   list($Username, $Domain) = split("@",$email);

   if(getmxrr($Domain, $MXHost)) 
   {
      return TRUE;
   }
   else 
   {
      if(fsockopen($Domain, 25, $errno, $errstr, 30)) 
      {
         return TRUE; 
      }
      else 
      {
         return FALSE; 
      }
   }
}

$email = $_POST['email_address'];
{?>

<form method="post" action="<? $_SERVER['PHP_SELF']; ?>">
Email<input type="text" name="email_address" size="30"/>
<input type="submit" value="Register">
</form>
<?}

if(checkEmail($email) == FALSE) 
{
   echo "E-mail entered is not valid.";
} 
else 
{
   echo "E-mail entered is valid.";
}
?>
I don't know if it's something i did wrong
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 13 August 2006 - 09:48 PM

Try:

<?php
function checkEmail($email) 
{
   if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 
   {
      return FALSE;
   }

   list($Username, $Domain) = split("@",$email);

   if(getmxrr($Domain, $MXHost)) 
   {
      return TRUE;
   }
   else 
   {
      if(fsockopen($Domain, 25, $errno, $errstr, 30)) 
      {
         return TRUE; 
      }
      else 
      {
         return FALSE; 
      }
   }
}
if (isset($_POST['submit'])) {
    $email = trim(strip_tags($_POST['email_address']));
    if(checkEmail($email) == FALSE) {
       echo "E-mail entered is not valid.";
    } else {
       echo "E-mail entered is valid.";
    }
} else {
?>

<form method="post" action="<? $_SERVER['PHP_SELF']; ?>">
Email<input type="text" name="email_address" size="30"/>
<input type="submit" name="submit" value="Register">
</form>
<?php
}
?>

Modified, tested, works for me.
Legend has it that reading the manual never killed anyone.
My site

#9 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 13 August 2006 - 10:28 PM

I tried that and i get this error now

Parse error: syntax error, unexpected '{' in /home/adrock/public_html/jack/email.php on line 27

I can't see why it's reporting that error

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#10 toplancers

toplancers
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 August 2006 - 10:30 PM

$regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$';
      if (eregi($regex, $email))
  {
  $key = 1;
  }
      else
  {
  echo("Please enter a valid Email address!");
  }


http://TopLancers.com
Professionals and Freelancers create your profile today!
http://TopLancers.com/forum
Join today!

#11 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 13 August 2006 - 10:55 PM

I tried that and i get this error now ...


100% my fault, I missed a closing ) in checking the $_POST['submit'].  My original code has been modified to correct it.
Legend has it that reading the manual never killed anyone.
My site

#12 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 14 August 2006 - 12:10 AM

Thanks.....that works perfectly

BTW...how long does it take to check an invalid email.  To check a valid one takes a split second but if i put a radom domain in it takes ages.
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#13 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 August 2006 - 12:18 AM

Unlike life, success is quicker than failure  ;D

I'm sure there's a technical reason though. Maybe it's because the valid domains you test with are all in the same zone as you whereas a random domain could exist anywhere on the planet ... or not at all, in which case there's possibly retry times involved with the socket access.  Perhaps someone who really knows can explain it to you.

A potential downside to checking the domain could be that it is completely valid but 'temporarily' inaccessible (nothing has 100% uptime) which would cause your script to think it didn't exist when it really did.  Also, don't forget that just because the domain name exists it doesn't mean the email address exists.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users