Validate Mail

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Validate Mail

Post by Kriek »

Ok I've got the basic form. On Post it sends the user to a .php file which checks to see if all the forms have been filled out. If not it echos the missing fields. Once the the user has inserted something in all the fields it will continue with the process and send the mail.

Now want I am wanting to do is Validate the E-mail addess typed into the field. I know how to Validate the E-mail, but not really add it into this current script. So I'm going to list all my code and see if someone can help me implement them all together, cool?

Ok here is the Form code.

Code: Select all

<form class="forms" name="form" method="post" action="Thanks.php">
<p class="bodymd">Your Name<br>
<input class="forms" type="text" name="Name">
</p>
<p class="bodymd">Your Email<br>
<input class="forms" type="text" name="Email">
</p>
<p class="bodymd">Comments or Questions<br>
<textarea name="Comments" class="forms" rows="5"></text>
</p>
<p class="bodymd">
<input class="button" type="submit" name="Submit" value="Submit">
<input class="button" type="reset" name="Reset" value="Clear Form">
</p>
</form>
After they enter the fields they are sent to a .php file with this.

Code: Select all

<?php

if (($Name == "") || ($Email == "") || ($Comments == ""))
&#123;
	echo "<form name=form method=post class=forms action=Thanks.php>";
	echo "<p class=bodymd>All three fields of this form are required.</p>";
	echo "<p class=bodymd>Fill in the ones you missed, they are listed below.</p>";
&#125;
if ($Name == "")
&#123;
	echo "<p class=bodymd>Your Name<br><input class=forms type=text name=Name></p>";
&#125;
else
&#123;
	echo "<input class=forms type=hidden name=Name value=$Name>";
&#125;
if ($Email == "")
&#123;
	echo "<p class=bodymd>Your Email<br><input class=forms type=text name=Email></p>";
&#125;
else
&#123;
	echo "<input class=forms type=hidden name=Email value=$Email>";
&#125;
if ($Comments == "")
&#123;
	echo "<p class=bodymd>Comments or Questions<br><textarea name=Comments rows=5 cols=40 class=forms></text></p>";
&#125;
else
&#123;
	echo "<input class=forms type=hidden name=Comments value=$Comments>";
&#125;

if (($Name == "") || ($Email == "") || ($Comments == ""))
&#123;
	echo "<input class=button type=submit name=Submit value=Submit>";
	echo "<input class=button type=reset name=Reset value=Clear Form>";
	echo "</form>";
&#125;
else
&#123;
	$message = "Name: $Name\nEmail: $Email\nComments: $Comments\n";
	$extra = "From: $Name\r\nReply-To: $Email\r\n";
	mail ("Kriek@jonkriek.com", "Website Email", $message, $extra);
	echo "<p class=bodymd>Thanks for your inguiry, $Name.</p>";
	echo "<p class=bodymd>A response will be sent to $Email as soon as possible.</p>";
&#125;
?>
Now here is what I am trying to hook up to Validate the e-mail.

Code: Select all

<?php function validate_email ($email) &#123; 

# Trim out 'null' characters 
$email = ereg_replace("\t","",$email); 
$email = ereg_replace("\r","",$email); 
$email = ereg_replace("\n","",$email); 
$email = ereg_replace(" ","",$email); 

# Email addresses are not case sensitive 
$email = strtolower($email); 

# Must contain '@' and '.' 
if ((!ereg("@",$email)) || (!ereg(".",$email))) &#123; 
return "1"; 
&#125; 
# Must be at least 7 characters long (x@y.com) 
elseif (strlen($email) < 7) &#123; 
return "2"; 
&#125; 
else &#123; 

# Split on the @ sign 
$parts = split("@", $email, 2); 

# First part is user 
$user = $parts&#1111;0]; 

# Second part is domain 
$domain = $parts&#1111;1]; 

# Domain must contain at least 1 dot. 
if (!ereg("\\.",$domain)) &#123; 
return "3"; 
&#125; 
# Must be a least 4 characters (z.com) 
elseif (strlen($domain) < 4) &#123; 
return "4"; 
&#125; 
# May not start with a dot 
elseif (ereg("^\\.",$domain)) &#123; 
return "5"; 
&#125; 
# May not have more than one dot in sequence 
elseif (ereg("\\.\\.",$domain)) &#123; 
return "6"; 
&#125; 
else &#123; 

# User must be at least 1 character long 
if (strlen($user) < 1) &#123; 
return "7"; 
&#125; 
# User cannot contain a comma 
elseif (ereg("\\,", $user)) &#123; 
return "8"; 
&#125; 
else &#123; 

# Split on the . character 
$parts = split("\\.", $domain); 

# There must be at least two parts to a domain (a sub domain would have 3 - a.b.com) 
if (count($parts) < 2) &#123; 
return "9"; 
&#125; 
else &#123; 

# First part is domain name 
$name_at = count($parts) - 2; 
$name = $parts&#1111;$name_at]; 

# Second part is domain extention 
$ext_at = count($parts) - 1; 
$ext = $parts&#1111;$ext_at]; 

$first_name = substr($name, 0, 1); 

# Name must be at least one character 
if (strlen($name) < 1) &#123; 
return "10"; 
&#125; 
# Name cannot be more than 26 characters 
if (strlen($name) > 26) &#123; 
return "11"; 
&#125; 
# Extention must be 2 or 3 characters (.nu - .com) 
elseif ((strlen($ext) > 3) || (strlen($ext) < 2)) &#123; 
return "12"; 
&#125; 
# Domain can only contain the following characters 
elseif (strspn($name, "abcdefghijklmnopqrstuvwxyz0123456789-.") != strlen($name)) &#123; 
return "13"; 
&#125; 
# Domain can only start with the following characters 
elseif (strspn($first_name, "abcdefghijklmnopqrstuvwxyz") < 1) &#123; 
return "14"; 
&#125; 
# Extension can only contain the follow characters 
elseif (strspn($ext, "abcdefghijklmnopqrstuvwxyz") != strlen($ext)) &#123; 
return "15"; 
&#125; 
else &#123; 

return $email; 

&#125; 

&#125; 

&#125; 

&#125; 

&#125; 

&#125; ?>
It's a pretty darn good script. Not even sure it will work.
Any help would be great.
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

Not to make you feel bad, but what is the point of an e-mail validation. Your not preventing anyone from putting in a false e-mail address. Your just slowing them down, if even that. Understand what I'm saying? If people are going to lie, they'll lie. If they never intended to fill out the form correctly, they'll just make up a false e-mail address.

So save yourself the trouble of trying to fix pointless things, no offense.
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

gotDNS wrote:Not to make you feel bad, but what is the point of an e-mail validation. Your not preventing anyone from putting in a false e-mail address. Your just slowing them down, if even that. Understand what I'm saying? If people are going to lie, they'll lie. If they never intended to fill out the form correctly, they'll just make up a false e-mail address.

So save yourself the trouble of trying to fix pointless things, no offense.
No offense taken.

This is partly for a client. So regardless if I agree that it's useful or not.
I need to get it done. So again .. any help would be great.
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

Once again, with calaborations between you and your client, you being the programmer, it may be a suggestion.

Just a suggestion to you... :wink:
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Kriek: Your Email validation function is way to long. :D However, got a shorter one, much more efficient, for you.

Code: Select all

<?php 

function is_email_valid($email)
&#123; 
  	if( eregi("^&#1111;a-z0-9\._-]+@+&#1111;a-z0-9\._-]+\.+&#1111;a-z]&#123;2,3&#125;$", $email) )
	&#123;
  		$return_data = true;
	&#125; else &#123;
		$return_data = false;
	&#125;

	return $return_data; 
&#125;

$empName = empty($Name);
$empEmail = empty($Email);
$empComments = empty($Comments);

if ( $empName || $empEmail || $empComments ) 
&#123; 
   echo "<form name=form method=post class=forms action=Thanks.php>"; 
   echo "<p class=bodymd>All three fields of this form are required.</p>"; 
   echo "<p class=bodymd>Fill in the ones you missed, they are listed below.</p>"; 
&#125; 

if ($empName)  
&#123; 
   echo "<p class=bodymd>Your Name<br><input class=forms type=text name=Name></p>"; 
&#125; else &#123; 
   echo "<input class=forms type=hidden name=Name value=$Name>"; 
&#125; 

if ($empEmail || !is_email_valid($Email) ) 
&#123; 
   echo "<p class=bodymd>Your Email<br><input class=forms type=text name=Email></p>"; 
&#125; else &#123; 
   echo "<input class=forms type=hidden name=Email value=$Email>"; 
&#125; 

if ($empComments) 
&#123; 
   echo "<p class=bodymd>Comments or Questions<br><textarea name=Comments rows=5 cols=40 class=forms></text></p>"; 
&#125; else &#123; 
   echo "<input class=forms type=hidden name=Comments value=$Comments>"; 
&#125; 

if ( $empName || $empEmail || $empComments ) 
&#123; 
   echo "<input class=button type=submit name=Submit value=Submit>"; 
   echo "<input class=button type=reset name=Reset value=Clear Form>"; 
   echo "</form>"; 
&#125; else &#123; 
   $message = "Name: $Name\nEmail: $Email\nComments: $Comments\n"; 
   $extra = "From: $Name\r\nReply-To: $Email\r\n"; 
   mail ("Kriek@jonkriek.com", "Website Email", $message, $extra); 
   echo "<p class=bodymd>Thanks for your inguiry, $Name.</p>"; 
   echo "<p class=bodymd>A response will be sent to $Email as soon as possible.</p>"; 
&#125; 
?>
This should work fine, in place of your code.

Note: If you are using PHP with the $_POST array, you should be using it. :D Makes for much better, much neater code.
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

That code still checks to make sure they enter something in all the fields, but doesn't validate the mail. It just sends mail after anything is typed in all the fields.

This is where I am testing it. Check it out. Again thanks for your time Jason.
Last edited by Kriek on Wed Jun 05, 2002 8:49 am, edited 2 times in total.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Oh jeez! Gah...stupid logic errors grrr....

Code: Select all

<?php 

function is_email_valid($email) 
&#123; 
     if( eregi("^&#1111;a-z0-9\._-]+@+&#1111;a-z0-9\._-]+\.+&#1111;a-z]&#123;2,3&#125;$", $email) ) 
   &#123; 
        $return_data = true; 
   &#125; else &#123; 
      $return_data = false; 
   &#125; 

   return $return_data; 
&#125; 

$empName = empty($Name); 
$empEmail = empty($Email); 
$empComments = empty($Comments);
$validEmail = is_email_valid($Email);

if ( $empName || $empEmail || $empComments ) 
&#123; 
   echo "<form name=form method=post class=forms action=Thanks.php>"; 
   echo "<p class=bodymd>All three fields of this form are required.</p>"; 
   echo "<p class=bodymd>Fill in the ones you missed, they are listed below.</p>"; 
&#125; 

if ($empName)  
&#123; 
   echo "<p class=bodymd>Your Name<br><input class=forms type=text name=Name></p>"; 
&#125; else &#123; 
   echo "<input class=forms type=hidden name=Name value=$Name>"; 
&#125; 

if ($empEmail ||  !$validEmail) 
&#123; 
   echo "<p class=bodymd>Your Email<br><input class=forms type=text name=Email></p>"; 
&#125; else &#123; 
   echo "<input class=forms type=hidden name=Email value=$Email>"; 
&#125; 

if ($empComments) 
&#123; 
   echo "<p class=bodymd>Comments or Questions<br><textarea name=Comments rows=5 cols=40 class=forms></text></p>"; 
&#125; else &#123; 
   echo "<input class=forms type=hidden name=Comments value=$Comments>"; 
&#125; 

if ( $empName || $empEmail || $empComments || !$validEmail ) 
&#123; 
   echo "<input class=button type=submit name=Submit value=Submit>"; 
   echo "<input class=button type=reset name=Reset value=Clear Form>"; 
   echo "</form>"; 
&#125; else &#123; 
   $message = "Name: $Name\nEmail: $Email\nComments: $Comments\n"; 
   $extra = "From: $Name\r\nReply-To: $Email\r\n"; 
   mail ("Kriek@jonkriek.com", "Website Email", $message, $extra); 
   echo "<p class=bodymd>Thanks for your inguiry, $Name.</p>"; 
   echo "<p class=bodymd>A response will be sent to $Email as soon as possible.</p>"; 
&#125; 
?>
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Ok now that seemed to validate the mail, but after 1-2 times of entering a invalid e-mail the submit and clear buttons stop working. It's like it disconnects from the form somehow. When I hit the back the buttons work, but it's right after it echoes the Email field.

Take a look here again.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Just switch this:

Code: Select all

if ( $empName || $empEmail || $empComments )
with this:

Code: Select all

if ( $empName || $empEmail || $empComments || !$validEmail )
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Awesome thanks Jason It works now. Like gotDNS said they can still fake the forum and the e-mail, but my friend/client wanted it. So he will be happy.

PS: I'll get to work on the banner, lol
Post Reply