View Full Version : form email validation

03-23-2004, 12:14 AM
How do I integrate an email validator into this form?
I've seen snippets from some other code, but what i've tried hasn't worked yet.
Basically want to validate an actual email.
Simple would be to make sure the @ and .com/.net etc is there, but there is some better ones that check the domain status and stuff.
Any ideas?


$form_block = "
<form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
<p><strong>Your name:</strong><br />
<input type=\"text\" name=\"sender_name\" value=\"$_POST[sender_name]\" size=30></p>
<p><strong>Your E-Mail Address:</strong><br />
<input type=\"text\" name=\"sender_email\" value=\"$_POST[sender_email]\" size=30></p>
<p><strong>Message:</strong><br />
<textarea name=\"message\" cols=30 rows=5 wrap=virtual>$_POST[message]</textarea></p>
<input type=\"hidden\" name=\"op\" value=\"ds\">
<p><input type=\"submit\" name=\"submit\" value=\"Send This Form\"></p>

if ($_POST[op] != "ds") {
// show form
echo "$form_block";
} else if ($_POST[op] == "ds") {
// check value of $_POST[sender_name]
if ($_POST[sender_name] == "") {
$name_err = "<div class=\"error\" align=\"center\">The Name field was left Blank</div><br />";
$send = "no";
// check value of $_POST[sender_email]
if ($_POST[sender_email] == "") {
$email_err = "<div class=\"error\" align=\"center\">The Email field was left Blank</div><br />";
$send = "no";
// check value of $_POST[message]
if ($_POST[message]== "") {
$message_err = "<div class=\"error\" align=\"center\">You did not enter a Message</div><br />";
$send = "no";
if ($send != "no") {
// it's ok to send, so construct the mail
$msg = "E-MAIL SENT FROM WWW SITE\n"; // body text build
$msg .= "Sender's name: $_POST[sender_name]\n";
$msg .= "Sender's E-Mail: $_POST[sender_email]\n";
$msg .= "Message: $_POST[message]\n\n";

$to = "sales@mydomain.com";
$subject = "Contact Form";
$mailheaders = "From: $_POST[sender_email]\n";
$mailheaders .= "Reply-To: $_POST[sender_email]\n\n";
// send the mail
mail($to, $subject, $msg, $mailheaders);
// display confirmation to user
echo "<p>Thank you $_POST[sender_name], your content has been sent!><br />
A member of our staff will be in contact with you as soon as possible.</p>";
} else if ($send == "no") {
// print error messages
echo "$name_err";
echo "$email_err";
echo "$message_err";
echo "$form_block";

03-23-2004, 12:29 AM
I believe there's a tutorial on www.zend.com which does check to see if the domain/email exists.

03-25-2004, 04:31 PM
you could do a quick one using javascript

var txtEmail = document.forms.frmName.txtEmail.value; // assign the value of the text box to txtEmail

if (txtEmail.indexOf("@") == -1 || txtEmail.indexOf(".") == -1) // check to see if email is 'valid'
alert("Sorry but you have entered an incorrect email address. Please try again.");

03-25-2004, 05:17 PM
I know ... but if i was to do that I'd use the behavior "form validation" in dreamweaver.

I'd like to incorporate it into the php functions so that i could have div tags appear under or next to the input box with the error. :thumbsup:

DHTML Kitchen
03-26-2004, 01:35 AM


03-26-2004, 02:17 AM
that one works for me...

thanks :thumbsup: