Form validation

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
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Form validation

Post by akimm »

The form validation below, for one reason or another does not validate my form input, I don't understand where my mistake it, probably in the elseif, but i wasn't sure how I could demonstrate to check everything, besides maybe pipelines, or && but i wasn't sure.

Code: Select all

<?php
if (empty($_POST['name'])) {
echo "Please fill out your name so we can give you credit for your lovely work";
}  elseif(empty($_POST['sender_email'])) {
echo "please enter a valid email.  Thank you";
}  elseif(empty($_POST['title']))  {
echo "We need a title for your poem : )";
} elseif(empty($_POST['poem'])) {
echo "the idea is to submit a poem , that requires a poem, thank you!";
}

$email = $_POST['sender_email'];
if (!eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 
{
   echo "You've entered an invalid email format, please provide an email like yourname@yahoo.com, Thank you ";
}
?>

Code: Select all

<form action="send_simpleform.php" name="msgform" method="POST">
<p><b>your name</b>
<input type="text" name="name" size="30"></p>

<p><b>your email</b>
<input type="text" name="sender_email" size="30"></p>

<p><b>your title</b>
<input type="text" name="title" size="30"></p>

<p><b>your poem</b>
<TEXTAREA NAME="poem" COLS="30" ROWS="5" WRAP="virtual"></TEXTAREA></P>

<p><input type="submit" name="submit" value="send your poem!"></p>
Last edited by akimm on Wed Aug 23, 2006 1:51 am, edited 2 times in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Is there a question in all of that?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Yes

Post by akimm »

Despite my obvious problem to relate the question.

I am wondering why when the form is submitted, with mistakes I know I am making like, emails '3i3i3i3' or spaces omitted, yet, the form processes anyway, despite my logic telling it to warn the user, so as to allow for the proper information to be put in.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So you are saying that the echo's are not echoing the messages you have in there?
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

Post by jito »

seems the problem is in the regx and i don't know much of that thing. why don't you use javascript for validation?
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

try

Code: Select all

if(empty($_POST['name'])) {
...
}

if(empty($_POST['email'])) {
...
}
instead of using elseif

With elseif, as soon as it evaluates true, all the other conditions are ignored.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

/**
 * Tests if the input matches a the basic syntaxical rules of an email address
 *
 * @return bool
 */
static public function email($value)
{
	return (bool)preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', trim($value));
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

feyd wrote:http://svn.gna.org/viewcvs/blacknova/tr ... iew=markup may be of interest for validating RFC compliant email addresses.
Image
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yeah I don't think that will work.

Code: Select all

$addr_spec = "$local_part \@ $X $domain";

Code: Select all

$mailbox .= "$phrase  $route_addr";  // name and address
Lots of nice spaces you have to match there.

and for a function called validateEmailFormat surprisingly little actually does that.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Thanks

Post by akimm »

I didn't know you could use if in continouslly, I appreciate the help; especially on the reg expression, cuz that <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> is over my head.
Post Reply