SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    email validation

    Hi, I have this code below and I want to add in a validation command, so the user needs to fill that space in.

    I don't know where to put this code in the php. Any help would be greatfull.
    PHP Code:
    function check_email ($email1) {
             return 
    (
    ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',$email1));

    Insert.php file
    PHP Code:
    <?php

    include("config.php");
    include(
    "global.php");

    mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
    mysql_select_db ($DBname);

    mysql_query ("INSERT INTO tellafriend (Name1, Email1, Date, Name2, Email2) values ('$name1', '$email1', (now()), '$name2', '$email2')");

    // Start Message to friend

    $message "This message is from your friend -- $name1 at $email1
    ----------
    $mainmsg
    "
    ;

        
    mail($email2,"$emailsubject",
             
    $message"From:$gbsitename <$adminemail>");

    ?>

  2. #2
    SitePoint Addict AbelaJohnB's Avatar
    Join Date
    Oct 2001
    Location
    Eureka, CA. USA
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try the following, I think you'll find it much better


    PHP Code:
    <?php

    include("config.php");
    include(
    "global.php");

    mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
    mysql_select_db ($DBname);



    ////////////////////////////////////////////////////////////
    // validate_email
    //
    // - Validates Email Address
    //
    // Usage:
    // if (!validate_email($email)) { echo 'Bad'; } else { echo 'Good'; }
    ////////////////////////////////////////////////////////////
    function validate_email($str_input){ 
       if (
    eregi("^([a-z]|[0-9]|\.|-|_)+@([a-z]|[0-9]|\.|-|_)+\.([a-z]){2,3}$"$str_input$arr_vars) && 
           !
    eregi("(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)"$str_input$arr_vars)){ 
          return 
    true
       }
    }

    if (!
    validate_email($Email2)) {
        echo 
    'Bad Email Address Entered';
    }
    ////////////////////////////////////////////////////////////

    mysql_query ("INSERT INTO tellafriend (Name1, Email1, Date, Name2, Email2) values ('$name1', '$email1', (now()), '$name2', '$email2')");

    // Start Message to friend

    $message "This message is from your friend -- $name1 at $email1
    ----------
    $mainmsg";

        
    mail($email2,"$emailsubject",
             
    $message"From:$gbsitename <$adminemail>");

    ?>



    Later.
    John B. Abela
    www.JohnAbela.Com

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2001
    Location
    Earth
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you can put that code in any file you want (as long as you include it).

    The you just would issue:

    if (check_email($email_address)
    {
    good
    }
    else
    {
    bad
    }

    Where $email_address is the address you want to check. You'll have to fill in the code for good and bad.

    Frank
    http://www.serverexpert.com/ Discuss servers and the software that makes them work.
    http://www.webmasters-resources.com/ Resources for webmasters.

  4. #4
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks John, can you help me a little more?

    I have added that code to the insert.php file but how would I go about adding $email1, $email2, $name1, $name2 to that list.

    Here is the live version, http://www.boxxnet.com.au/tellafriend/submit.php
    No matter what you enter it always comes up with that error. "Bad Email Address Entered"

  5. #5
    SitePoint Addict AbelaJohnB's Avatar
    Join Date
    Oct 2001
    Location
    Eureka, CA. USA
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Dean Wilson
    Thanks John, can you help me a little more?

    I have added that code to the insert.php file but how would I go about adding $email1, $email2, $name1, $name2 to that list.

    Here is the live version, http://www.boxxnet.com.au/tellafriend/submit.php
    No matter what you enter it always comes up with that error. "Bad Email Address Entered"

    First, I made one slight mistake with the code above. It should be:

    PHP Code:
    if (!validate_email($Email2)) {
        echo 
    'Bad Email Address Entered';
     exit();



    I have added that code to the insert.php file but how would I go about adding $email1, $email2, $name1, $name2 to that list.

    By that do you mean if you enter multiple email addresses into the "send to" field??

    If not, what do you mean?

    No matter what you enter it always comes up with that error. "Bad Email Address Entered" [/B]

    Well, almost impossible to tell you what you did wrong, because you have not provided me/us with the code that you have...

    Telling you what it wrong, without the code, is somewhat like having to guess where a single submarine in the entire Atlantic ocean



    Later
    John B. Abela
    www.JohnAbela.Com

  6. #6
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    John, there is 4 feilds. $email1 $email2, $name1 and $name2 are all different feilds.

    I was it so the user has to enter his email and his friends email -- Here is the full code.
    PHP Code:
    <?php

    include("config.php");
    include(
    "global.php");

    mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
    mysql_select_db ($DBname);

    ////////////////////////////////////////////////////////////
    // validate_email
    //
    // - Validates Email Address
    //
    // Usage:
    // if (!validate_email($email)) { echo 'Bad'; } else { echo 'Good'; }
    ////////////////////////////////////////////////////////////
    function validate_email($str_input){ 
       if (
    eregi("^([a-z]|[0-9]|.|-|_)+@([a-z]|[0-9]|.|-|_)+.([a-z]){2,3}$"$str_input$arr_vars) && 
           !
    eregi("(@.*@)|(..)|(@.)|(.@)|(^.)"$str_input$arr_vars)){ 
          return 
    true
       }
    }


    if (!
    validate_email($Email2)) {
        echo 
    'Bad Email Address Entered';
     exit();
    }
    ////////////////////////////////////////////////////////////

    mysql_query ("INSERT INTO tellafriend (Name1, Email1, Date, Name2, Email2) values ('$name1', '$email1', (now()), '$name2', '$email2')");

    // Start Message to friend

    $message "This message is from your friend -- $name1 at $email1
    ----------
    $mainmsg
    "
    ;

        
    mail($email2,"$emailsubject",
             
    $message"From:$gbsitename <$adminemail>");

    ?>
    <body bgcolor="<?=$gbcolor3?>" text="<?=$gbtextcolor?>">
    <div align="center"><font face="Arial" size="4"><b><br>Thank you!<br></b></font>
    <font face="Arial" size="2"><br>Here are the details you have submitted.<br>
    <br></font> </div>
    <div align="center">
      <table width="600" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td>
            
            <table width="600" border="0" cellspacing="2" cellpadding="7">
              <tr> 
                <td width="152" bgcolor="<?=$gbcolor1?>"> 
                  <div align="right"><font size="2" face="Arial"><b>Your Name:</b></font></div>
                </td>
                <td width="413" bgcolor="<?=$gbcolor1?>"><font face="Arial" size="2"> 
                  <? echo "" $name1 "" ?>
                  </font></td>
              </tr>
              <tr> 
                <td width="152" bgcolor="<?=$gbcolor2?>"> 
                  <div align="right"><font size="2" face="Arial"><b>Your Email:</b></font></div>
                </td>
                <td width="413" bgcolor="<?=$gbcolor2?>"><font face="Arial" size="2"> 
                  <? echo "" $email1 "" ?>
                  </font></td>
              </tr>
              <tr> 
                <td width="152" bgcolor="<?=$gbcolor1?>"> 
                  <div align="right"><font size="2" face="Arial"><b>Friends Name:</b></font></div>
                </td>
                <td width="413" bgcolor="<?=$gbcolor1?>"> <font face="Arial" size="2"> 
                  <? echo "" $name2 "" ?>
                  </font></td>
              </tr>
              <tr> 
                <td width="152" bgcolor="<?=$gbcolor2?>"> 
                  <div align="right"><font size="2" face="Arial"><b>Friends Email:</b></font></div>
                </td>
                <td width="413" bgcolor="<?=$gbcolor2?>"> <font face="Arial" size="2"> 
                  <? echo "" $email2 "" ?>
                  </font></td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
      <br>
    </div>

    <table width="600" border="0" cellspacing="0" cellpadding="0" align="center">
      <tr> 
        <td> 
          <hr size="2">
        </td>
      </tr>
    </table>
    <table width="600" border="0" cellspacing="0" cellpadding="0" align="center">
      <tr> 
        <td width="50%"> <font face="Arial, Helvetica, sans-serif" size="2"><b><a href="<? echo "$gbsiteurl?>"> 
          <? echo "Return to $gbsitename?>
          </a></b></font></td>
        <td width="50%">
          <div align="right"><font size="2" face="Arial"><b><a href="submit.php">Tell 
            another friend</a></b></font></div>
        </td>
      </tr>
    </table>

  7. #7
    SitePoint Addict AbelaJohnB's Avatar
    Join Date
    Oct 2001
    Location
    Eureka, CA. USA
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simply Syntax Error that one of us really should have caught! (CASE-SENSITIVE!!)


    PHP Code:
    <?php

    include("config.php");
    include(
    "global.php");

    mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
    mysql_select_db ($DBname);

    ////////////////////////////////////////////////////////////
    // Validate Email Address:  < [email]scripts@4cm.com[/email] >
    ////////////////////////////////////////////////////////////
    // validate_email
    //
    // - Validates Email Address
    //
    // Usage:
    // if (!validate_email($email)) { echo 'Bad'; } else { echo 'Good'; }
    ////////////////////////////////////////////////////////////
    function validateEmail($email){ 
       if (
    eregi("^([a-z]|[0-9]|\.|-|_)+@([a-z]|[0-9]|\.|-|_)+\.([a-z]|[0-9]){2,3}$"$email$arr_vars) && 
           !
    eregi("(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)"$email$arr_vars)){ 
          return 
    true
       } else { 
          return 
    false
       } 
    }


    if (!
    validateEmail($HTTP_POST_VARS[email1]) ) {
        echo 
    'Your Email Address Is Incorrect';
      exit;
    }
    if (!
    validateEmail($HTTP_POST_VARS[email2]) ) {
        echo 
    'Your Friends Email Address Is Incorrect';
      exit;
    }
    ////////////////////////////////////////////////////////////

    mysql_query ("INSERT INTO tellafriend (Name1, Email1, Date, Name2, Email2) values ('$name1', '$email1', (now()), '$name2', '$email2')");

    // Start Message to friend

    $message "This message is from your friend -- $name1 at $email1
    ----------
    $mainmsg
    "
    ;

        
    mail($email2,"$emailsubject",
             
    $message"From:$gbsitename <$adminemail>");

    ?>
    <body bgcolor="<?=$gbcolor3?>" text="<?=$gbtextcolor?>">
    <div align="center"><font face="Arial" size="4"><b><br>Thank you!<br></b></font>
    <font face="Arial" size="2"><br>Here are the details you have submitted.<br>
    <br></font> </div>
    <div align="center">
      <table width="600" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td>
            
            <table width="600" border="0" cellspacing="2" cellpadding="7">
              <tr> 
                <td width="152" bgcolor="<?=$gbcolor1?>"> 
                  <div align="right"><font size="2" face="Arial"><b>Your Name:</b></font></div>
                </td>
                <td width="413" bgcolor="<?=$gbcolor1?>"><font face="Arial" size="2"> 
                  <? echo "" $name1 "" ?>
                  </font></td>
              </tr>
              <tr> 
                <td width="152" bgcolor="<?=$gbcolor2?>"> 
                  <div align="right"><font size="2" face="Arial"><b>Your Email:</b></font></div>
                </td>
                <td width="413" bgcolor="<?=$gbcolor2?>"><font face="Arial" size="2"> 
                  <? echo "" $email1 "" ?>
                  </font></td>
              </tr>
              <tr> 
                <td width="152" bgcolor="<?=$gbcolor1?>"> 
                  <div align="right"><font size="2" face="Arial"><b>Friends Name:</b></font></div>
                </td>
                <td width="413" bgcolor="<?=$gbcolor1?>"> <font face="Arial" size="2"> 
                  <? echo "" $name2 "" ?>
                  </font></td>
              </tr>
              <tr> 
                <td width="152" bgcolor="<?=$gbcolor2?>"> 
                  <div align="right"><font size="2" face="Arial"><b>Friends Email:</b></font></div>
                </td>
                <td width="413" bgcolor="<?=$gbcolor2?>"> <font face="Arial" size="2"> 
                  <? echo "" $email2 "" ?>
                  </font></td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
      <br>
    </div>

    <table width="600" border="0" cellspacing="0" cellpadding="0" align="center">
      <tr> 
        <td> 
          <hr size="2">
        </td>
      </tr>
    </table>
    <table width="600" border="0" cellspacing="0" cellpadding="0" align="center">
      <tr> 
        <td width="50%"> <font face="Arial, Helvetica, sans-serif" size="2"><b><a href="<? echo "$gbsiteurl?>"> 
          <? echo "Return to $gbsitename?>
          </a></b></font></td>
        <td width="50%">
          <div align="right"><font size="2" face="Arial"><b><a href="submit.php">Tell 
            another friend</a></b></font></div>
        </td>
      </tr>
    </table>

    Anyway, the above should work just fine

    Later.
    John B. Abela
    www.JohnAbela.Com


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
  •