email format validation
Posted: Tue Aug 25, 2009 11:58 pm
I found the below code on this site, http://www.plus2net.com/php_tutorial/ph ... dation.php
I would like to work it into this code below (of course I will add a form field for email):
Code: Select all
if(isset($todo) and $todo=="test"){
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
echo "<center>Invalid email</center>";
}else{
echo "<center>Valid Email</center>";}
}
Code: Select all
<?
session_start();
include("membersarea/includes/outside_top_navi.php");
include("database.php");
/**
* Returns true if the username has been taken
* by another user, false otherwise.
*/
function usernameTaken($username){
global $conn;
if(!get_magic_quotes_gpc()){
$username = addslashes($username);
}
$q = "select username from Users where username = '$username'";
$result = mysql_query($q,$conn);
return (mysql_numrows($result) > 0);
}
/**
* Inserts the given (username, password) pair
* into the database. Returns true on success,
* false otherwise.
*/
function addNewUser($acctid, $username, $password, $createdt, $enddt, $status){
global $conn;
$q = "INSERT INTO Users VALUES ('$acctid', '$username', '$password', '$createdt', '$enddt', '$status')";
return mysql_query($q,$conn);
}
/**
* Displays the appropriate message to the user
* after the registration attempt. It displays a
* success or failure status depending on a
* session variable set during registration.
*/
function displayStatus(){
$uname = $_SESSION['reguname'];
if($_SESSION['regresult']){
?>
<table style="width: 100%;" height="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="49%" bgcolor="E3E3E3">
</td>
<td width="871" valign="top">
<table width="871" height="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="871" valign="top">
<h1>Registered!</h1>
<p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="loginpage.php" title="Login">log in</a>.</p>
</td>
</tr>
</table>
</td>
<td width="49%" bgcolor="E3E3E3">
</td>
</tr>
</table>
</body>
</HTML>
<?
}
else{
?>
<!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>
<title></title>
</head>
<table style="width: 100%;" height="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="49%" bgcolor="E3E3E3">
</td>
<td width="871" align="top">
<table width="871" height="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="871" valign="top">
<h1>Registration Failed</h1>
<p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br>
Please try again at a later time.</p>
</td>
</tr>
</table>
</td>
<td width="49%" bgcolor="E3E3E3">
</td>
</tr>
</table>
</body>
</HTML>
<?
}
unset($_SESSION['reguname']);
unset($_SESSION['registered']);
unset($_SESSION['regresult']);
}
if(isset($_SESSION['registered'])){
/**
* This is the page that will be displayed after the
* registration has been attempted.
*/
?>
<!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>
<title></title>
</head>
<table style="width: 100%;" height="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="49%" bgcolor="E3E3E3">
</td>
<td width="871" align="top">
<table width="871" height="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="871" valign="top">
<title>Registration Page</title>
<? displayStatus(); ?>
</td>
</tr>
</table>
</td>
<td width="49%" bgcolor="E3E3E3">
</td>
</tr>
</table>
</body>
</HTML>
<?
return;
}
/**
* Determines whether or not to show to sign-up form
* based on whether the form has been submitted, if it
* has, check the database for consistency and create
* the new account.
*/
if(isset($_POST['subjoin'])){
/* Make sure all fields were entered */
if(!$_POST['user'] || !$_POST['pass']){
die('You didn\'t fill in a required field.');
}
/* Spruce up username, check length */
$_POST['user'] = trim($_POST['user']);
if(strlen($_POST['user']) > 30){
die("Sorry, the username is longer than 30 characters, please shorten it.");
}
/* Check if username is already in use */
if(usernameTaken($_POST['user'])){
$use = $_POST['user'];
die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
}
/* Add the new account to the database */
$acctid = '';
$md5pass = md5($_POST['pass']);
$date = date("Y-m-d");
$enddt = '';
$status = '00';
$_SESSION['reguname'] = $_POST['user'];
$_SESSION['regresult'] = addNewUser($acctid, $_POST['user'], $md5pass, $date, $enddt, $status);
$_SESSION['registered'] = true;
echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
return;
}
else{
/**
* This is the page with the sign-up form, the names
* of the input fields are important and should not
* be changed.
*/
?>
<!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>
<title></title>
</head>
<table style="width: 100%;" height="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="49%" bgcolor="E3E3E3">
</td>
<td width="871">
<table width="871" height="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="871" valign="top">
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td> </td><td><? print(Date("m/d/Y")); ?></td></tr>
<tr><td> </td><td><h1>Register</h1></td></tr>
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
</table>
</form>
</td>
</tr>
</table>
</td>
<td width="49%" bgcolor="E3E3E3">
</td>
</tr>
</table>
</body>
</HTML>
<?
}
?>