validating email address

Hi all,

I’m new to php, I already made a php page for email blast but i want to add a validating email script into the existing script, does anybody know how to do it? greatly appreciate your assistance, thanks :slight_smile: :slight_smile:

here is the code


$name = β€œβ€;
$email = β€œβ€;
$msg_to_user = β€œβ€;
if ($_POST[β€˜name’] != β€œβ€) {

include_once "connect_to_mysql.php";

// Be sure to filter this data to deter SQL injection, filter before querying database
$name = $_POST['name'];
$email = $_POST['email'];

$sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
$numRows = mysql_num_rows($sql);

if (!$email) {
	$msg_to_user = '<br /><br /><h4><font color="FF0000">Please type an email address ' . $name . '.</font></h4>';

} else if ($numRows > 0) {
	$msg_to_user = '<br /><br /><h4><font color="FF0000">' . $email . ' is already in the system.</font></h4>';
} else {
	$sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime) 
											    VALUES('$name','$email',now() )")  or die (mysql_error());
	$msg_to_user = '<br /><br /><h4><font color="0066FF">Thanks ' . $name . ', you have been added successfully.</font></h4>';
	$name = "";
    $email = "";



Subscribe to Our Newsletter Subscribe to Our Newsletter Β 


<?php echo $msg_to_user; ?>


check out jquery validate, its really easy to use :slight_smile:

Hi there,

If you aren’t worried about doing on-the-fly validation using javascript as suggested by herghost, you could use a regular expression in your PHP:

[php]if (!preg_match("/^([a-zA-Z0-9])[email protected]([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/", $email))
$msg_to_user = β€œInvalid e-mail address”;

Sponsor our Newsletter | Privacy Policy | Terms of Service