• home
  • forum
  • my
  • kt
  • download
  • How to validate an email address with PHP

    Author: 2008-08-24 10:37:18 From:

    One php page named contactPage.php will contain all the code. Let抯 create a simple contact form.


    <form name="contact" action="contactPage.php" method="POST" enctype="multipart/form-data" >

    Name:<br />
    <input type="text" name="name" />
    <br />

    Email:<br />
    <input type="text" name="email"  />
    <br />

    Comments:<br />
    <textarea name="comments"></textarea>
    <br />

    <input name="process" type="hidden" value="1" />
    <input type="submit" value="Verify!" />
    </form>


    The action parameter of the form tag, tells our form to return to the same page after the user presses the submit button. We have three fields that the user can fill out: name, email and comments.

    We also have a hidden field called 損rocess?with the value of 1. This field will allow our script to check if the form has been submitted as you will see later.

    Ok, so we got our form. Now let抯 code the part where the script processes the data entered into the form. For simplification purposes, we are going to ignore the name and comments fields. Also, we will just display 慪ay! You entered a valid email address?as a success message.

    <?php
    if($_POST['process']==1)
    {
        
    // IF THE EMAIL FIELD WAS LEFT BLANK
        
    if(empty($_POST['email']))
        {
            echo 
    '<p style="color:#C00">Please enter your email address.</p>';
        }
        
    // IF USER ENTERED SOMETHING INTO THE EMAIL FIELD
        
    else
        {
            
    // IF USER ENTERED AN INVALID EMAIL ADDRESS
            
    if(preg_match('/.*@.*\..*/',  $_POST['email']) > 0)
            {
                echo 
    'Yay! You entered a valid email address!';
            }
            
    // IF USER ENTERED A VALID EMAIL ADDRESS
            
    else
            {
                echo 
    '<p style="color:#C00">Please enter a valid email address.</p>';
            }
        }
    }
    ?>


    if($_POST[憄rocess抅==1) checks if the form has been submitted.

    If the form has been submitted, then it makes sure the user did not leave the email field blank by using the empty function.

    The preg_match function searches what the user entered as an email address and checks if it matches the regular expression. If preg_match returns a 0 it means there was no match. If it returns a 1 it means there is at least one match.

    Of course, the code does not let you know if the email address actually exists. It only makes sure you entered an email address in a valid format which would be the email, then the @ character, then the domain name, then a period and then something else (email@domain.extension).

    I hope you liked this article. Please submit your comments by using the form below.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (19)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (24)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (54)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot