Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Where could I find a script that validates an email address when users register?
Old 07-02-2010, 01:40 AM Where could I find a script that validates an email address when users register?
Skilled Talker

Posts: 83
Trades: 0
I'm trying to create a professional script that validates that an email typed in a text field by the user when they are registering, is an actual email address. So, if the email address is missing the @ sign or doesn't meet the typical parameters that an email should have, it will not pass and the user will have to type in a valid email address. This is pretty advanced for me. Any scripts you guys use that you would like to share? Or, where could I find a script that does something like this?
Smudly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-02-2010, 03:50 AM Re: Where could I find a script that validates an email address when users register?
Phunk Rabbit's Avatar
Ultra Talker

Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
Trades: 0
You just need to run the email field through a regex checker.

PHP Code:

<?php



$email 
"someone@example.com";



if(
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email)) {

  echo 
"Valid email address.";

}

else {

  echo 
"Invalid email address.";

}



?>
Source http://www.totallyphp.co.uk/code/val...xpressions.htm
__________________

Please login or register to view this content. Registration is FREE

Learn professional skills from professional people, from $6.50 a month.
Phunk Rabbit is offline
Reply With Quote
View Public Profile Visit Phunk Rabbit's homepage!
 
Old 07-02-2010, 05:04 PM Re: Where could I find a script that validates an email address when users register?
Skilled Talker

Posts: 83
Trades: 0
Hey thanks for the reply. I'm having some troubles with my email validator however. When I hit submit, I keep getting the error "Enter a Valid Email", even if the email address is valid. There is something wrong with my structure but I can't figure it out. Here is the code I'm working with. The area that is having trouble is:

PHP Code:
    //////////// Email Validation ////////////
function validEmail($email)
{
   
$isValid true;
   
$atIndex strrpos($email"@");
   if (
is_bool($atIndex) && !$atIndex)
   {
      
$isValid false;
   }
   else
   {
      
$domain substr($email$atIndex+1);
      
$local substr($email0$atIndex);
      
$localLen strlen($local);
      
$domainLen strlen($domain);
      if (
$localLen || $localLen 64)
      {
         
// local part length exceeded
         
$isValid false;
      }
      else if (
$domainLen || $domainLen 255)
      {
         
// domain part length exceeded
         
$isValid false;
      }
      else if (
$local[0] == '.' || $local[$localLen-1] == '.')
      {
         
// local part starts or ends with '.'
         
$isValid false;
      }
      else if (
preg_match('/\\.\\./'$local))
      {
         
// local part has two consecutive dots
         
$isValid false;
      }
      else if (!
preg_match('/^[A-Za-z0-9\\-\\.]+$/'$domain))
      {
         
// character not valid in domain part
         
$isValid false;
      }
      else if (
preg_match('/\\.\\./'$domain))
      {
         
// domain part has two consecutive dots
         
$isValid false;
      }
      else if
(!
preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 
str_replace("\\\\","",$local)))
      {
         
// character not valid in local part unless 
         // local part is quoted
         
if (!preg_match('/^"(\\\\"|[^"])+"$/',
             
str_replace("\\\\","",$local)))
         {
            
$isValid false;
         }
      }
      if (
$isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         
// domain not found in DNS
         
$isValid false;
      }
   }
   return 
$isValid;
}

        if (
validEmail($email)==false){
        
$proerror "Enter a Valid Email";
    }
    else{
    
//////////// End Email Validation /////////
    
    
$update "UPDATE `users` SET `fname`='$fnamenew', `lname`='$lnamenew', `email`='$emailnew' WHERE `username`='$username'";
    
mysql_query($update);
    
$success "Success!";
    } 
And here is my entire code:

PHP Code:
<?php
session_start
();
if (isset(
$_SESSION['username'])){

include(
'inc/connect.php');



$username = isset($_SESSION['username']) ? $_SESSION['username'] : ''

$edit = (isset($_POST['edit']));
$passchange = (isset($_POST['passchange']));

if (!empty(
$username))

    
//if user is logged in 

    
$sql mysql_query("SELECT * FROM `users` WHERE `username`='$username'"); 
    
$row mysql_fetch_assoc($sql); 

    
$dbfname $row['fname']; 
    
$dblname $row['lname']; 
    
$dbemail $row['email']; 
    
$dbpassword $row['password'];
    
$passcapture $_POST['password']; 
    
$password md5($passcapture);
    
    
$error "";
    
$proerror "";
    
    if (
$edit)
    {
    
        
$fnamenew mysql_real_escape_string(strtolower(strip_tags($_POST['fname']))); 
        
$lnamenew mysql_real_escape_string(strtolower(strip_tags($_POST['lname']))); 
        
$emailnew mysql_real_escape_string(strip_tags($_POST['email'])); 
        

        
    
    
//////////// Email Validation ////////////
function validEmail($email)
{
   
$isValid true;
   
$atIndex strrpos($email"@");
   if (
is_bool($atIndex) && !$atIndex)
   {
      
$isValid false;
   }
   else
   {
      
$domain substr($email$atIndex+1);
      
$local substr($email0$atIndex);
      
$localLen strlen($local);
      
$domainLen strlen($domain);
      if (
$localLen || $localLen 64)
      {
         
// local part length exceeded
         
$isValid false;
      }
      else if (
$domainLen || $domainLen 255)
      {
         
// domain part length exceeded
         
$isValid false;
      }
      else if (
$local[0] == '.' || $local[$localLen-1] == '.')
      {
         
// local part starts or ends with '.'
         
$isValid false;
      }
      else if (
preg_match('/\\.\\./'$local))
      {
         
// local part has two consecutive dots
         
$isValid false;
      }
      else if (!
preg_match('/^[A-Za-z0-9\\-\\.]+$/'$domain))
      {
         
// character not valid in domain part
         
$isValid false;
      }
      else if (
preg_match('/\\.\\./'$domain))
      {
         
// domain part has two consecutive dots
         
$isValid false;
      }
      else if
(!
preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 
str_replace("\\\\","",$local)))
      {
         
// character not valid in local part unless 
         // local part is quoted
         
if (!preg_match('/^"(\\\\"|[^"])+"$/',
             
str_replace("\\\\","",$local)))
         {
            
$isValid false;
         }
      }
      if (
$isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         
// domain not found in DNS
         
$isValid false;
      }
   }
   return 
$isValid;
}

        if (
validEmail($email)==false){
        
$proerror "Enter a Valid Email";
    }
    else{
    
//////////// End Email Validation /////////
    
    
$update "UPDATE `users` SET `fname`='$fnamenew', `lname`='$lnamenew', `email`='$emailnew' WHERE `username`='$username'";
    
mysql_query($update);
    
$success "Success!";
    }

    
$dbfname $fnamenew
    
$dblname $lnamenew
    
$dbemail $emailnew
    
    
    
    
    }
    
    
// Change Password
    
if ($passchange)
    {
            if(
$password){
            
                if(
$password==$dbpassword){
                
                    
$passwordnew $_POST['passwordnew']; 
                    
$passwordconf $_POST['passwordconf'];
                    
                    if (isset(
$passwordnew) && !empty($passwordnew)){
                    
                        if (isset(
$passwordconf) && !empty($passwordconf)){
                            if (
strlen($passwordnew)>=&& strlen($passwordconf)>=6){
                            
                                if (
$passwordnew==$passwordconf){
                                    
                                    
$passwordnew md5($passwordnew);
                                    
                                
$passupdate "UPDATE `users` SET `password`='$passwordnew' WHERE `username`='$username'";
                                
mysql_query($passupdate);
                                
                                
$passsuccess "Success!";
                                
                            }
                            else{
                                
$error "Your new password does not match!";
                            }
                            }
                            else{
                                
$error "Your new password must contain at least 6 characters!";
                            }
                        }
                        else{
                            
$error "Please type in your Confirmed Password!";
                        }
                     
                    }
                    else{
                        
$error "Please type in your New Password!";
                    }
                
                }
                else{
                    
$error "Invalid Password";
                }
        
        
        
            }
            else{
                
$error "Please type in your Password!";
            }
    
    
    
    }
    
}
}
else{
    
header("Location: index.php");
}
?>

<html> 
<head> 
<title>Profile</title> 


<script type="text/javascript" language="javascript">
function inputLimiter(e,allow) {
var AllowableCharacters = '';

if (allow == 'UserNameChar'){AllowableCharacters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';}
if (allow == 'UsernameChar'){AllowableCharacters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';}

var k;

k=document.all?parseInt(e.keyCode): parseInt(e.which);

if (k!=13 && k!=8 && k!=0){

if ((e.ctrlKey==false) && (e.altKey==false)) {

return (AllowableCharacters.indexOf(String.fromCharCode(k))!=-1);

} else {

return true;

}

} else {

return true;

}

}

</script>
<style>
#container{
    width: 275px;
    margin-left: auto;
    margin-right: auto;
}
#profile{
    width: 222px;
    text-align: right;
    margin-left: auto;
    margin-right: auto;
    
}
#changepassword{
    width: 268px;
    text-align: right;
    margin-left: auto;
    margin-right: auto;
    
}
#centerpro{
    width: 60px;
    margin-left: auto;
    margin-right: auto;
}
#centerpas{
    width: 120px;
    margin-left: auto;
    margin-right: auto;
}
#center{
    width: 150px;
    margin-left: auto;
    margin-right: auto;
    text-align: center;
}
</style>
</head> 
<body> 
<div id="container">
<div id="profile">
    <h3 align="center">Profile</h3>
<form action="profile.php" method="POST"> 
    Username: <input type="text" value="<?php echo ucfirst($username); ?>" readonly="readonly"><br /> 
    First Name: <input type="text" maxlength="25" id="UserNameChar" onkeypress="return inputLimiter(event,'UserNameChar')" name="fname" value="<?php echo ucfirst($dbfname); ?>"><br /> 
    Last Name: <input type="text" maxlength="25" id="UserNameChar" onkeypress="return inputLimiter(event,'UserNameChar')" name="lname" value="<?php echo ucfirst($dblname); ?>"><br /> 
    Email: <input type="text" maxlength="64" name="email" value="<?php echo ucfirst($dbemail); ?>"><br />
    <div id="centerpro"><input type="submit" name="edit" value="Submit"></div>
    <div id="center"><?php echo $success$proerror?></div>
</div>    
    <br />
    <br />
<div id="changepassword">
    <h3 align="center">Change Password</h3>
    Password: <input type="password" maxlength="25" name="password"><br /><br /> 
    New Password: <input type="password" maxlength="25" name="passwordnew"><br /> 
    Confirm Password: <input type="password" maxlength="25" name="passwordconf"><br /> 
    <div id="centerpas"><input type="submit" name="passchange" value="Change Password"></div>
    <div id="center"><?php echo $passsuccess$error?></div>
</form> 
</div>
</div>
</body> 
</html>
Smudly is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Where could I find a script that validates an email address when users register?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.67538 seconds with 12 queries