Jump to content

Validate email address


bobby317

Recommended Posts

I am trying to validate the email address on my register form but can't seem to get it here is what I have. Any help would be appreciated. Also please explain so I can learn thanks.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>

<style type="text/css">

body {
text-align: center;
}

form {
text-align: left;
}

label
{
width: 8em;
float: left;
text-align: right;
margin-right: 0.5em;

}

.submit input {
margin-left: 7.5em;
}

.error {
color: #F00;
}

</style>

</head>

<body>
<?php

//Starts the code when form is submitted:
if( !isset($_POST['submit'])){
include_once "regform.php";
} else {
//flag variable to track success:
$okay = TRUE;

//Email pattern variable
$regexp = "^([_a-z0-9-] )([\. ][_a-z0-9-] )*@([a-z0-9-] )(\.[a-z0-9-] )*(\.[a-z]{2,4})$";

//Validate the email address:
if (empty($_POST['email1'])) {
	print '<p class="error">Please enter your email.</p>';
	$okay = FALSE;
	include_once "regform.php";
}

//Checks email for correct pattern
elseif (eregi($regexp, $_POST['email1'])) {
	print'<p class="error">You email is not valid.</p>';
	$okey = FALSE;
	include_once "regorm.php";
}

//Validate the password:
elseif (empty($_POST['pass1'])) {
	print '<p class="error">Please enter your password.</p>';
	$okay = FALSE;
	include_once "regform.php";
}

//Valadate legnth of password
elseif (strlen($_POST['pass1']) < 6) {
	print'<p class="error">Your password must be atleast 6 charachters long.</p>';
	$okay = FALSE;
	include_once "regform.php";
}

//validate the emails for equality:
elseif ($_POST['email1'] != $_POST['email2']) {
	print '<p class="error">Your emails do not match.</p>';
	$okay = FALSE;
	include_once "regform.php";
}

//Validate the passwords for equality:
elseif ($_POST['pass1'] != $_POST['pass2']) {
	print '<p class="error">Your passwords do not match.</p>';
	$okay = FALSE;
	include_once "regform.php";
}

//If there were no errors, print a success message:
elseif ($okay == TRUE) {

	//Trims email and password and sets to a varible:
	$email = trim($_POST['email1']);
	$password = trim($_POST['pass1']);

	//Encript password using email as salt:
	$password = sha1($email.$password);	

	//Include files for conecting to database:
	$dbc = mysql_connect('rwddesign.com:3306', 'rwddesi1_bobby31', 'jessica');
	mysql_select_db('rwddesi1_test');

	//Define the query:
	$query = "INSERT INTO users (userID, email, password) VALUES (0, '$email', '$password')";

	//Execute the query:
	if (@mysql_query($query)) {

		//Print message if secsessful
		print '<h1>You have registered</h1>';

	} else {

		//Get error number
		$errorNumber = mysql_errno();

		//print message if duplacate email
		if ( $errorNumber == 1062 ) {
			print '<h1 class="error">Email is already registered please try again.</h1>';
			include_once "regform.php";

		} else {

		//print message for all other errors.
		print '<h1 class="error">Could not register because:' . mysql_error() . ' .</h1>
			   <p class="error">The query being run was: ' . $query . '</p>';
			   include_once "regform.php";
	}
	}
	mysql_close();	
}
}

?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/201444-validate-email-address/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.