SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Valid email

  1. #1
    Non-Member
    Join Date
    Jul 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Valid email

    I have a register.php file and currently it lets any email, even 'hello'. I know it's possible to make sure that the email field contains @ and . which makes up a valid email.

    Can anyone spill the beans?

  2. #2
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Try this..
    PHP Code:
        if(!(eregi"([_\.0-9a-z-]+@)([0-9a-z][0-9a-z-]+\.)+([a-z]{2,3})"$email)))
        {
            
    //not a valid email
        

    -- Jelena --

  3. #3
    Non-Member
    Join Date
    Jul 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure whether I have that code in the right place or not.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html;charset=iso-8859-2" />
    <link rel="stylesheet" href="images/style.css" type="text/css" />
    <title>MusicInterests</title>
    </head>
    <body>
        <div class="content">
            <div class="header_right">
                <div class="top_info">
                    <div class="top_info_right">
                        <p><?php include("logged.php"); ?></p>                    
                    </div>        
                </div>
                        
                <div class="bar">
                    <ul>
                        <li class="slogan">Navigation:</li>
    <?php include("navigation.php"); ?>
                    
                    </ul>
                </div>
            </div>
                
            <div class="logo">
                <h1><a href="#" title="The best music on the net!">Music <span class="red">Interests</span></a></h1>
                <p>What are you listening too?</p>
            </div>
            
            <div class="search_field">
                <?php include("searchform.php"); ?>
            </div>
            
            <div class="newsletter">
                <p>Subscribe for the Newsletter!</p>
            </div>
            
            <div class="subheader">
                <p><?php include("top.php"); ?></p>
            </div>
            
            <div class="left">
                <div class="left_articles">
                    <h2>Sign up!</h2>
                    <p><?php
    ob_start
    ();
    // allows you to use cookies
    include("config.php");
    //gets the config page
    if ($_POST[register]) {
    // the above line checks to see if the html form has been submitted
    $username $_POST[username];
    $password $_POST[pass];
    $cpassword $_POST[cpass];
    $email $_POST[emai1];
    if(!(
    eregi"([_\.0-9a-z-]+@)([0-9a-z][0-9a-z-]+\.)+([a-z]{2,3})"$email)))
        {
            
    //not a valid email
        

    $artist1 $_POST[artist1];
    $song1 $_POST[song1];
    $artist2 $_POST[artist2];
    $song2 $_POST[song2];
    $artist3 $_POST[artist3];
    $song3 $_POST[song3];
    $artist4 $_POST[artist4];
    $song4 $_POST[song4];
    $artist5 $_POST[artist5];
    $song5 $_POST[song5];
    $artist6 $_POST[artist6];
    $song6 $_POST[song6];
    $artist7 $_POST[artist7];
    $song7 $_POST[song7];
    $artist8 $_POST[artist8];
    $song8 $_POST[song8];
    $artist9 $_POST[artist9];
    $song9 $_POST[song9];
    $artist10 $_POST[artist10];
    $song10 $_POST[song10];
    //the above lines set variables with the user submitted information
    if($username==NULL|$password==NULL|$cpassword==NULL|$email==NULL|$artist1==NULL|$song1==NULL|$artist2==NULL|$song2==NULL|$artist3==NULL|$song3==NULL|$artist4==NULL|$song4==NULL|$artist5==NULL|$song5==NULL|$artist6==NULL|$song6==NULL|$artist7==NULL|$song7==NULL|$artist8==NULL|$song8==NULL|$artist9==NULL|$song9==NULL|$artist10==NULL|$song10==NULL) {
    //checks to make sure no fields were left blank
    echo "<font color=\"red\">A field was left blank or you did not enter all your songs.<br /><br />
    <a href=\"#\" onClick=\"history.go(-1)\">Click here to go back</a> 
    </font>
    "
    ;
    }else{
    //none were left blank!  We continue...
    if($password != $cpassword) {
    // the passwords are not the same!  
    echo "<font color=\"red\">Passwords do not match.</font> <br /><br />
    <a href=\"#\" onClick=\"history.go(-1)\">Click here to go back</a>
    "
    ;
    }else{
    // the passwords are the same!  we continue...
    $password md5($password);

    // encrypts the password
    $checkname mysql_query("SELECT username FROM users WHERE username='$username'");
    $checknamemysql_num_rows($checkname);
    $checkemail mysql_query("SELECT email FROM users WHERE email='$email'");
    $checkemail mysql_num_rows($checkemail);
    if (
    $checkemail>0|$checkname>0) {

    // oops...someone has already registered with that username or email!
    echo "<font color=\"red\">The username or email is already in use.</font><br /><br />
    <a href=\"#\" onClick=\"history.go(-1)\">Click here to go back</a> 
    "
    ;
    }else{
    // noone is using that email or username!  We continue...
    $username htmlspecialchars($username);
    $password htmlspecialchars($password);
    $email htmlspecialchars($email);
    $song1 htmlspecialchars($song1);
    $artist1 htmlspecialchars($artist1);
    $song2 htmlspecialchars($song2);
    $artist2 htmlspecialchars($artist2);
    $song3 htmlspecialchars($song3);
    $artist3 htmlspecialchars($artist3);
    $song4 htmlspecialchars($song4);
    $artist4 htmlspecialchars($artist4);
    $song5 htmlspecialchars($song5);
    $artist5 htmlspecialchars($artist5);
    $song6 htmlspecialchars($song6);
    $artist6 htmlspecialchars($artist6);
    $song7 htmlspecialchars($song7);
    $artist7 htmlspecialchars($artist7);
    $song8 htmlspecialchars($song8);
    $artist8 htmlspecialchars($artist8);
    $song9 htmlspecialchars($song9);
    $artist9 htmlspecialchars($artist9);
    $song10 htmlspecialchars($song10);
    $artist10 htmlspecialchars($artist10);
    // the above lines make it so that there is no html in the user submitted information.
    //Everything seems good, lets insert.
    $query mysql_query("INSERT INTO users (username, password, email, song1, artist1, song2, artist2, song3, artist3, song4, artist4, song5, artist5, song6, artist6, song7, artist7, song8, artist8, song9, artist9, song10, artist10) VALUES('$username','$password','$email','$song1','$artist1','$song2','$artist2','$song3','$artist3','$song4','$artist4','$song5','$artist5','$song6','$artist6','$song7','$artist7','$song8','$artist8','$song9','$artist9','$song10','$artist10')");
    // inserts the information into the database.
    echo "You have successfully registered!";
    }
    }
    }
    }
    else
    {
    // the form has not been submitted...so now we display it.
    echo ("

    <form method=\"POST\">
    Username:<br><input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\"><br />
    Password:<br><input type=\"password\" size=\"15\" maxlength=\"25\" name=\"pass\"><br />
    Confirm Password:<br><input type=\"password\" size=\"15\" maxlength=\"25\" name=\"cpass\"><br />
    Email:<br><input type=\"text\" size=\"15\" maxlength=\"25\" name=\"emai1\"><br /><br />
    Please add your songs, you can always go back and edit or change them if you make a mistake or update your collection. Please spell the artist and songs as well as possible.<br><br>
    <b>Song one</b><br>
    Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist1\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song1\"><br />
    <b>Song two</b><br>
    Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist2\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song2\"><br />
    <b>Song three</b><br>
    Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist3\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song3\"><br />
    <b>Song four</b><br>
    Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist4\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song4\"><br />
    <b>Song five</b><br>
    Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist5\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song5\"><br />
    <b>Song six</b><br>
    Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist6\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song6\"><br />
    <b>Song seven</b><br>
    Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist7\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song7\"><br />
    <b>Song eight</b><br>
    Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist8\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song8\"><br />
    <b>Song nine</b><br>
    Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist9\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song9\"><br />
    <b>Song ten</b><br>
    Artist - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"artist10\"> Song - <input type=\"text\" size=\"19\" maxlength=\"25\" name=\"song10\"><br />

    <br /><input name=\"register\" type=\"submit\" value=\"Register\">
    </form>

    "
    );
    }
    ?> 
    </p>
                </div>
            </div>    
            <div class="right">
                            
                <div class="rt"></div>
                <div class="right_articles">
                    <p><b>Most viewed</b><br />The most viewed tool allows you too find the 30 most viewed profiles on musicinterests!</p>
                </div>
                <div class="rt"></div>
                <div class="right_articles">
                    <p><b>Links</b><br />
                    <?php include("links.php"); ?></p>
                </div>
                <div class="rt"></div>
                <div class="right_articles">
                    <p><?php include("sponsor.php"); ?></p>
                </div>
            </div>    
            <div class="footer">
                <p><?php include("footer.php"); ?></p>
            </div>
        </div>
    </body>
    </html>
    If that is the right place then unfortunately it doesn't seem to be working.

  4. #4
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code is fine, but you don't have it doing anything if it fails. If it fails it is doing absoluteley nothing within the if statement.

    You need to have the script alert the user that the e-mail is not valid, and keep the script from inserting information into the database.
    ]

  5. #5
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it should work if you include the code to be executed when validation fails.

  6. #6
    Non-Member
    Join Date
    Jul 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    huh? i dont have any code stopping users from putting un-valid emails in

  7. #7
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a variety of ways you can do this. You can have an $error variable be set to true if validation fails. You can then run this variable in a conditional statement so,
    PHP Code:
     if ($error == TRUE) { //display error
    } else {
    //mysql insertions

    Make any sense?

    There are more effecient ways to do this, but this is a good start.
    ]


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
  •