www.webdeveloper.com
Results 1 to 2 of 2

Thread: function email

  1. #1
    Join Date
    Jul 2004
    Posts
    44

    function email

    I am trying to learn how to implement a function into my code. I have not had much experience in this, so would appreciate a hand with my code just so I can understand this process as its something that I need to grip.

    I have an error check to make sure they fill the email field in on a form. What I next need to do is integrate a email check to make sure the email is valid and not just any old data.

    The problem I have is understanding how to call the function and how to show an error and where it all goes together.

    Here is my code with the function included its not the whole script just what is relevant.

    PHP Code:
            if ( empty($email) ) {
            
    $error['email_error'] = '<div class="formerror">Please enter your email</div>';
            }     

     
    // End of error checking, all fields covered.

     
    if (!$error) {
     
    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;

     
     
      
    $sql "SELECT * FROM tableA WHERE username = '$_POST[username]' "
      
    $sql_result mysql_query($sql); 
     
      if (
    mysql_num_rows($sql_result) !=0)

    etc.. etc.. 
    Here is the html code from the form again just the section for email.

    Code:
     <p><label for="email">Email</label> <input class="long" type="text" value="<?php echo $email; ?>" name="email" id="email" /><br><?php echo $error['email_error']; ?></p>
    I want to understand how to use the function in my form. Once I grasp this, I think I will understand so much better and perform better.

  2. #2
    Join Date
    Oct 2005
    Location
    Cambridgeshire, UK
    Posts
    191
    Your function does not necessarily need to be in an 'if' loop. It can be placed anywhere in your page. The top of your code is the normal place. To get this to work you need to call the function and do something based on it's return. In this case, if the function returns false (ie. invalid email address) then do add an error message to the $error array. Amend your code as follows:

    PHP Code:
    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 ( empty(
    $email) ) {
        
    $error['email_error'] = '<div class="formerror">Please enter your email</div>';
    }else{
        if (!
    validEmail($email)) { // if function returns false
            
    $error['email_error'] = '<div class="formerror">Please enter a valid email address</div>';
        }
    }
      
    // End of error checking, all fields covered.

    if (!$error) {

      
    $sql "SELECT * FROM tableA WHERE username = '$_POST[username]' ";
      
    $sql_result mysql_query($sql);

      if (
    mysql_num_rows($sql_result) !=0
    Let me know if you have any questions about the above. Please note, I haven't tested this but should be a good starting point.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles