Checking an email FORM

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
Drenkhahn
Forum Newbie
Posts: 1
Joined: Wed Mar 19, 2003 1:03 pm

Checking an email FORM

Post by Drenkhahn »

Ok, I have this PHP set up to take a name and email address and write it to a text file. It works great. However, I'd like to have it check to make sure its not an empty form (or better yet, maybe check to make sure there is a "@" somewhere in there) before it allows them to proceed.

Any hints?

Here's what I have now..




<?
if($Submit) //check for form submission
{

//compile the information to store within the textfile
$str_tofile = "name: $name\n";
$str_tofile.= "emailaddress: $emailaddress\n";
//open a file pointer to the text file where you want to save the submitted form data
$filepointer = fopen('maillist.txt','a+');
//save the submitted form data to the file
fwrite($filepointer,$str_tofile);
//thank the user
echo ?>blablabla

<form name="form1" method="post" action="maillist.php">
<table width="346" height="110" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>Email Address</td>
<td><input name="emailaddress" type="text" id="emailaddress"></td>
</tr>
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

Check out validateEmailFormat from Killersoft http://www.killersoft.com/downloads/paf ... on=viewall
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

javascript would probably be even easier...
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

This will check for an "@" and, after the "@", at least one "." but no more than two "." I have been told that you can have more than two "." after the "@" - although I've never seen that.

Code: Select all

$domain = strstr($email, '@');
        IF (!strstr($email, '@') OR substr_count($domain, ".") == 0  OR substr_count($domain, ".") > 2) { ..etc
I've seen whole classes devoted to email validation using regex'ing and sometimes also checking for valid domains.

Can anyone tell me why you'd want to regex? Since special characters are used in email addresses, surely you have to allow them all, using something nice and simple similar to the above code.
Last edited by McGruff on Thu Aug 11, 2005 1:15 pm, edited 1 time in total.
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

regex use in email validation is useful to ensure that the email: A) has a valid pattern (address@place.ext, add.re.ss@place.ext.loc, etc); and B) does not contain illegal characters. For example: name@place.org is fine, but name@place@site.org is not.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Thanks Daven. I didn't take account of more than one @ in the above code. Should have added another substr_count().

I was trying to think of a way to validate without regex'ing but a strstr() and a few substr_count() 's maybe isn't going to be any faster.
Post Reply