Jump to content

Archived

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

AdRock

Checking for valid email address format

Recommended Posts

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
[code]<?
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) {[/code]
I want to put the validation after the end of this code.

In one exapmple i looked at from http://www.spoono.com/php/tutorials/tutorial.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

Share this post


Link to post
Share on other sites
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.com/tutorials/40/0.php

Hope it helps anyway

Share this post


Link to post
Share on other sites
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 @

Share this post


Link to post
Share on other sites
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

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

Share this post


Link to post
Share on other sites
@ScottRiley- Is the following string a valid email- "@$#^^.."?

Here is what I use:
[code]<?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!";
}
?>[/code]

Orio.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I got some code from [url=http://www.spoono.com/php/tutorials/tutorial.php?id=41]http://www.spoono.com/php/tutorials/tutorial.php?id=41[/url] 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

[code]<?
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.";
}
?>[/code]
I don't know if it's something i did wrong

Share this post


Link to post
Share on other sites
Try:

[code]<?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
}
?>[/code]

Modified, tested, works for me.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
$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!

Share this post


Link to post
Share on other sites
[quote author=AdRock link=topic=104080.msg415152#msg415152 date=1155508125]
I tried that and i get this error now ...[/quote]

100% my fault, I missed a closing ) in checking the $_POST['submit'].  My original code has been modified to correct it.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.