SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru prequel's Avatar
    Join Date
    Nov 1999
    Location
    Brisbane, Australia
    Posts
    682
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form mail email validation?

    Okay i am a php newbie and have been following tutorials on creating my own form mail for one of my sites... i have everything except email validation. How do i add email validation to the below script
    PHP Code:
    <?php

    $form 
    "
    <FORM METHOD=\"post\" ACTION=\"
    $PHP_SELF\">
    <p>Name: 
    <input type=\"text\" name=\"name\" value=\"
    $name\" size=\"30\">
    <br>
    Email: 
    <input type=\"text\" name=\"email\" value=\"
    $email\" size=\"30\">
    <br>
    </p>
    <p> 
    <textarea name=\"comments\" rows=\"12\" cols=\"45\" wrap=\"virtual\">
    $comments</textarea>
    </p>
    <p>                    
    <INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"check\">
    <INPUT TYPE=\"reset\" VALUE=\"reset\">
    <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Submit\"></p>
    </FORM>
    "
    ;

    if (
    $action != "check") {
    // show form
    echo "$form";
    } elseif (
    $action == "check") {

    $send "yes";

    if (
    $name == "") {
    $name_error "<br><b>Please enter name...</b><br>";
    $send "no";
    }

    if (
    $email == "") {
    $email_error "<b>Please enter email...</b><br>";
    $send "no";
    }

    if (
    $comments == "") {
    $comments_error "<b>Please enter a message...</b><br>";
    $send "no";
    }

    if (
    $send == "yes") {
    // okay to send
    $msg "Email sent from domain.com form\n\n";
    $msg .= "Sender's Name:    $name\n";
    $msg .= "Sender's Email:   $email\n";
    $msg .= "Message:          $comments\n";

    $to "form@domain.com";
    $subject "Contact Form";
    $mailheaders "From: domain.com <> \n";
    $mailheaders .= "Reply: $email\n\n";
    // send email
    mail($to$subject$msg$mailheaders);
    echo 
    "<br><br>Mail has been sent. ";

    $msg2 "Email sent from domain.com form\n\n";
    $msg2 .= "Sender's Name:    $name\n";
    $msg2 .= "Sender's Email:   $email\n";
    $msg2 .= "Message:          $comments\n";
    $confirm "Copy of form email sent: domain.com";
    $mailheaders2 "From: domain.com <> \n";
    $mailheaders2 .= "Reply: $to\n\n";
    mail($email$confirm$msg2$mailheaders2);
    echo 
    "A copy has also been sent to your email";

    } elseif (
    $send == "no") {
    echo 
    "$name_error";
    echo 
    "$email_error";
    echo 
    "$comments_error";
    echo 
    "$form";
     }
    }
    ?>
    thanks

  2. #2
    SitePoint Enthusiast kcabobert's Avatar
    Join Date
    Aug 2001
    Location
    Topeka, KS
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like to use a function to validate the addy, then I would add emval="yes" to if ($send == "yes"). So it would look like:
    PHP Code:
    if ($send == "yes" and emval="yes"

    //the validation function would look similar to:

    function vmail($email) { 
      if(
    eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$"$email)){
          return 
    "yes"
      } else {
          return 
    "no"
      }
    }

    // I would aslo do this:

    if ($name == "" && $email == "" && $comments == "") {
    echo (
    "<br><b>All required feilds were not filled out, please return and try again...</b><br>");
    $send "no";
    }

    $emval vmail($email);
    if (
    $send == "yes" and $emval="yes"
    I like that because it makes the code more compact, but it takes the ability to say "you didn't fill out name" You could acctually merge those two if statements like this:

    PHP Code:
    $emval vmail($email);

    if (
    $name != "" && $email != "" && $comments != "" && $emval="yes") {

    /*send the mail*/

    } else {
    echo (
    "<br><b>All required feilds were not filled out or the email you entered was invalid; please return and try again...</b><br>");
    $send "no";


    I hope I answered your question, sorry about the extra info.
    Microsoft's Motto: Resistance is futile, you will be assimilated.

    My dog's name is Jade; she is a Miniature pinscher.
    Click here to see some of her pictures


Bookmarks

Posting Permissions

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