Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Regular Coder
    Join Date
    May 2006
    Posts
    128
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How to add email validation to existing code?

    I have a mailing list script that I have been using for some years now. I got it from the web and thought that it validated the email (Note: I am not very fluent in php) until I started receiving all sort of garbage through the mailing list which were not valid email addresses. Can somebody please tell me how to amend the code in such a way that it prevents people from sending invalid emails.

    PHP Code:
    <?php
    if(isset($_REQUEST['add_email'])){//if the form has been submitted, then process the e-mail address.
        
    if(isset($_REQUEST['email'])){//check that an email address has been entered.
            
    $email$_REQUEST['email'];//assign the email address to the $email variable.
        
    }else{
            
    $emailNULL// if there is no email address then make the $email variable blank/ NULL.
        
    }
        function 
    email($to$from$subject$message){//A little function to properly format the email (should work without) 
            
    $lb="\r\n";
            
    $header "From: ".$from;
            
    $header.= $lb;
            
    $header.='MIME-Version: 1.0';
            
    $header.='Content-type: text/html; charset=iso-8859-1';
            
    mail($to$subject$message$header);
        }
        
    $to'info@yourhost.com';//Email address to send details to.
        
    $subject'Mailing list submission';
        
    $message_client='You have signed up to the mailing list to receive updates';
        
    $message_admin="The following email address has signed up to the mailing list: $email ";
        
        if(
    $email){//if there is an email address
            
    $email_adminemail($to$email$subject$message_admin);//send the email to the site admin.
            
    $email_clientemail($email$to$subject$message_client);//Send a confirmation to the client.
                        
            
    if(!$email_admin){//if the email has been sent, display a message.
                
    echo '<P class="quotesMain">Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided.</P>';
            }else{
                echo 
    '<P class="quotesMain">There seems to have been a system error, please go back and try again. Sorry for any inconvenience caused.</P>';
            }
        }else{
            echo 
    '<P class="quotesMain">There has been a problem, please click back and try again. Sorry for any inconvenience caused.</P>';
        }
    }else{
    //If the form has not been submitted, display a message.
        
    echo '<P class="quotesMain">Enter your email address to receive updates.
                 Your information will not be shared with third parties.</P>'
    ;
    }
    ?>
                        <!--end of PHP code for the mailing list form -->
                        <!-- Display the form -->
                        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" class="quotesMain">
                          <INPUT name="email" type="text" class="form" value="<?php echo $_REQUEST['email'];?>" />
                          <INPUT name="add_email" type="submit" class="formbutton" value="submit" />
                        </form>

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    I uses this excellent tutorial and script - http://www.ilovejackdaniels.com/php/...ss-validation/

  • #3
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts
    PHP Code:
      $checkfrom "^.+@.+\.com$|.ca$|.net$|.info$|.org$";
        if(!
    ereg($check_email$email))
          {
              echo 
    "<br>Please enter a valid email address";
          } 

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I second the ILoveJackDaniels script, I use it and it works fine. Though, there's no way you can avoid someone just making up an email address such as aaaaaaa@aaaaaaaaaaa.com-- to validate the email address you have to send an email to it with some sort of "click here to verify" link in it, or something similar, depending on your application.

  • #5
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Quote Originally Posted by Fumigator View Post
    I second the ILoveJackDaniels script, I use it and it works fine. Though, there's no way you can avoid someone just making up an email address such as aaaaaaa@aaaaaaaaaaa.com-- to validate the email address you have to send an email to it with some sort of "click here to verify" link in it, or something similar, depending on your application.
    I believe someone has actually posted that as a snippet in the PHP snippet section. I too recommend the ILoveJackDaniels script
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #6
    Regular Coder
    Join Date
    May 2006
    Posts
    128
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi guys,

    I have tried to add the email validator to my existing code but somehow can't get it to work. Can anybody please tell me where I should put the validator script in my existing script and also do I need to delete part of my script. Thanks

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If you are trying out the ILoveJackDaniels script, simply copy/paste the script from the link posted above into a separate PHP file. Include that file in your main PHP script. Then check an email address by calling that function:
    PHP Code:
    if (!check_email_address($emailString)) {
        echo 
    "email is invalid!";


  • #8
    Regular Coder
    Join Date
    May 2006
    Posts
    128
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I am using the JackDaniels script. I have placed it in an external php file. However, I don't know the syntax to insert an external php file into an existing script. I have looked around for tutorials that could show me how to do that but couldn't find any. Could you please show me how to write the correct syntax.

    Thanks

  • #9
    Regular Coder
    Join Date
    May 2006
    Posts
    128
    Thanks
    3
    Thanked 0 Times in 0 Posts
    OK...I have tried to incorporate the JackDaniels script directly into my existing script and when I test it within an invalid email address such as 'tommy', the message "Email is invalid" is displayed together with the following message "Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided."!!!

    Actually irrespective of whether I put a valid address or not, a message still gets sent. Can somebody please tell me what I'm doing wrong and what do I need to change in the existing script.

    Below is how I have added the JD script to my existing script:

    PHP Code:
    <?php
    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(isset(
    $_REQUEST['add_email'])){//if the form has been submitted, then process the e-mail address.
        
    if(isset($_REQUEST['email'])){//check that an email address has been entered.
            
    $email$_REQUEST['email'];//assign the email address to the $email variable.
        
    }else{
            
    $emailNULL// if there is no email address then make the $email variable blank/ NULL.
        
    }    
        
        
        function 
    email($to$from$subject$message){//A little function to properly format the email (should work without) 
            
    $lb="\r\n";
            
    $header "From: ".$from;
            
    $header.= $lb;
            
    $header.='MIME-Version: 1.0';
            
    $header.='Content-type: text/html; charset=iso-8859-1';
            
    mail($to$subject$message$header);
        }    
        
        
    $to'info@yourhost.com';//Email address to send details to.
        
    $subject'Mailing list submission';
        
    $message_client='You have signed up to the mailing list to receive updates to courses timetable';
        
    $message_admin="The following email address has signed up to the mailing list: $email ";    
        
        if(
    $email){//if there is an email address
        
    if (!check_email_address($emailString)) {
        echo 
    "email is invalid!";
    }  
            
    $email_adminemail($to$email$subject$message_admin);//send the email to the site admin.
            
    $email_clientemail($email$to$subject$message_client);//Send a confirmation to the client.
                        
            
    if(!$email_admin){//if the email has been sent, display a message.
                
    echo '<P class="quotesMain">Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided.</P>';
            }else{
                echo 
    '<P class="quotesMain">There seems to have been a system error, please go back and try again. Sorry for any inconvenience caused.</P>';
            }
        }else{
            echo 
    '<P class="quotesMain">There has been a problem, please click back and try again. Sorry for any inconvenience caused.</P>';
        }
    }else{
    //If the form has not been submitted, display a message.
        
    echo '<P class="quotesMain">Enter your email address to receive updates about new courses.
                 Your information will not be shared with third parties.</P>'
    ;
    }
    ?>
                        <!--end of PHP code for the mailing list form -->
                        <!-- Display the form -->
                        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" class="quotesMain">
                          <INPUT name="email" type="text" class="form" value="<?php echo $_REQUEST['email'];?>" />
                          <INPUT name="add_email" type="submit" class="formbutton" value="submit" />
                        </form>
    Last edited by forgottenglory; 09-06-2007 at 12:56 PM.

  • #10
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    add an else statement to this block
    PHP Code:
    if (!check_email_address($emailString)) {
        echo 
    "email is invalid!";
    }else{ 
    //add else
    //send the email....


  • #11
    Regular Coder
    Join Date
    May 2006
    Posts
    128
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I've added it as you suggested (see code below). An invalid email is not sent. However, as soon as the page loads the message "email is invalid" displays on top of the field. Any suggestions how to fix this?

    PHP Code:
    <?php
    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(isset(
    $_REQUEST['add_email'])){//if the form has been submitted, then process the e-mail address.
        
    if(isset($_REQUEST['email'])){//check that an email address has been entered.
            
    $email$_REQUEST['email'];//assign the email address to the $email variable.
        
    }else{
            
    $emailNULL// if there is no email address then make the $email variable blank/ NULL.
        
    }    
        
        
        function 
    email($to$from$subject$message){//A little function to properly format the email (should work without) 
            
    $lb="\r\n";
            
    $header "From: ".$from;
            
    $header.= $lb;
            
    $header.='MIME-Version: 1.0';
            
    $header.='Content-type: text/html; charset=iso-8859-1';
            
    mail($to$subject$message$header);
        }    
        
        
    $to'info@yourhost.com';//Email address to send details to.
        
    $subject'Mailing list submission';
        
    $message_client='You have signed up to the mailing list to receive updates to courses timetable';
        
    $message_admin="The following email address has signed up to the mailing list: $email ";    
        
       if (!
    check_email_address($emailString)) {
        echo 
    "email is invalid!";
    }else{ 
    //add else
    //send the email....
      
            
    $email_adminemail($to$email$subject$message_admin);//send the email to the site admin.
            
    $email_clientemail($email$to$subject$message_client);//Send a confirmation to the client.
                        
            
    if(!$email_admin){//if the email has been sent, display a message.
                
    echo '<P class="quotesMain">Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided.</P>';
            }else{
                echo 
    '<P class="quotesMain">There seems to have been a system error, please go back and try again. Sorry for any inconvenience caused.</P>';
            }
        }else{
            echo 
    '<P class="quotesMain">There has been a problem, please click back and try again. Sorry for any inconvenience caused.</P>';
        }
    }else{
    //If the form has not been submitted, display a message.
        
    echo '<P class="quotesMain">Enter your email address to receive updates about new courses.
                 Your information will not be shared with third parties.</P>'
    ;
    }
    }
    ?>
                        <!--end of PHP code for the mailing list form -->
                        <!-- Display the form -->
                        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" class="quotesMain">
                          <INPUT name="email" type="text" class="form" value="<?php echo $_REQUEST['email'];?>" />
                          <INPUT name="add_email" type="submit" class="formbutton" value="submit" />
                        </form>
    Last edited by forgottenglory; 09-06-2007 at 12:57 PM.

  • #12
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    because your not checking to see if the $email variable is set.
    PHP Code:
    $email $_POST['email']; // not sure if you set email before :)
    if($email){//if there is an email address
        
    if (!check_email_address($email)) {
        echo 
    "email is invalid!";
    }else{ 
    //send mail

    your also using to different variable names for email, you should just use one.

  • #13
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    heres how I validate emails on my site.

    PHP Code:
    function checkEmail($email){
             
    // checks proper syntax
             
    if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" $email)) {
                  
    // gets domain name
                  
    list($username,$domain)=split('@',$email);
                  
    // checks for if MX records in the DNS
                  
    if(!checkdnsrr($domain'MX')) {
                      return 
    false;
                  }
                  
    // attempts a socket connection to mail server
                  
    if(!fsockopen($domain,25,$errno,$errstr,30)) {
                    
    //if Domain false
                       
    return false;
                  }
                
    //If Domain vaild
                  
    return true;
             }
            else{
                
    //not correct format
                 
    return false;
            }
        } 

  • #14
    Regular Coder
    Join Date
    May 2006
    Posts
    128
    Thanks
    3
    Thanked 0 Times in 0 Posts
    @moo3: where in my original script would you fit your validation script for it to work properly? Thanks.

  • #15
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    <?php
    function checkEmail($email){
             
    // checks proper syntax
             
    if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" $email)) {
                  
    // gets domain name
                  
    list($username,$domain)=split('@',$email);
                  
    // checks for if MX records in the DNS
                  
    if(!checkdnsrr($domain'MX')) {
                      return 
    false;
                  }
                  
    // attempts a socket connection to mail server
                  
    if(!fsockopen($domain,25,$errno,$errstr,30)) {
                    
    //if Domain false
                       
    return false;
                  }
                
    //If Domain vaild
                  
    return true;
             }
            else{
                
    //not correct format
                 
    return false;
            }
        } 
    if(isset(
    $_REQUEST['add_email'])){//if the form has been submitted, then process the e-mail address.
        
    if(isset($_REQUEST['email'])){//check that an email address has been entered.
            
    $email$_REQUEST['email'];//assign the email address to the $email variable.
        
    }else{
            
    $emailNULL// if there is no email address then make the $email variable blank/ NULL.
        
    }
    if (isset([
    'add_email']) && !checkEmail($email)){
    $error "No email address entered";
    die(
    $error);
    }else{
        function 
    email($to$from$subject$message){//A little function to properly format the email (should work without) 
            
    $lb="\r\n";
            
    $header "From: ".$from;
            
    $header.= $lb;
            
    $header.='MIME-Version: 1.0';
            
    $header.='Content-type: text/html; charset=iso-8859-1';
            
    mail($to$subject$message$header);
        }
        
    $to'info@yourhost.com';//Email address to send details to.
        
    $subject'Mailing list submission';
        
    $message_client='You have signed up to the mailing list to receive updates';
        
    $message_admin="The following email address has signed up to the mailing list: $email ";
        
        if(
    $email){//if there is an email address
            
    $email_adminemail($to$email$subject$message_admin);//send the email to the site admin.
            
    $email_clientemail($email$to$subject$message_client);//Send a confirmation to the client.
                        
            
    if(!$email_admin){//if the email has been sent, display a message.
                
    echo '<P class="quotesMain">Thank you, your e-mail has been sent. You will receive a confirmation message via the e-mail you address provided.</P>';
            }else{
                echo 
    '<P class="quotesMain">There seems to have been a system error, please go back and try again. Sorry for any inconvenience caused.</P>';
            }
        }else{
            echo 
    '<P class="quotesMain">There has been a problem, please click back and try again. Sorry for any inconvenience caused.</P>';
        }
    }else{
    //If the form has not been submitted, display a message.
        
    echo '<P class="quotesMain">Enter your email address to receive updates.
                 Your information will not be shared with third parties.</P>'
    ;
    }
    }
    ?>
                        <!--end of PHP code for the mailing list form -->
                        <!-- Display the form -->
                        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" class="quotesMain">
                          <INPUT name="email" type="text" class="form" value="<?php echo $_REQUEST['email'];?>" />
                          <INPUT name="add_email" type="submit" class="formbutton" value="submit" />
                        </form>
    try that


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •