Page 1 of 1

Checking an email FORM

Posted: Wed Mar 19, 2003 1:03 pm
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>

Posted: Wed Mar 19, 2003 2:35 pm
by daven
Check out validateEmailFormat from Killersoft http://www.killersoft.com/downloads/paf ... on=viewall

Posted: Wed Mar 19, 2003 2:49 pm
by Jade
javascript would probably be even easier...

Posted: Wed Mar 19, 2003 3:57 pm
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.

Posted: Wed Mar 19, 2003 9:55 pm
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.

Posted: Thu Mar 20, 2003 2:12 am
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.