Page 1 of 1

Form validation

Posted: Wed Aug 23, 2006 1:49 am
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>

Posted: Wed Aug 23, 2006 1:50 am
by RobertGonzalez
Is there a question in all of that?

Yes

Posted: Wed Aug 23, 2006 1:53 am
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.

Posted: Wed Aug 23, 2006 2:14 am
by RobertGonzalez
So you are saying that the echo's are not echoing the messages you have in there?

Posted: Wed Aug 23, 2006 3:45 am
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?

Posted: Wed Aug 23, 2006 4:42 am
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.

Posted: Wed Aug 23, 2006 6:41 am
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));
}

Posted: Wed Aug 23, 2006 9:19 am
by feyd
feyd wrote:http://svn.gna.org/viewcvs/blacknova/tr ... iew=markup may be of interest for validating RFC compliant email addresses.
Image

Posted: Wed Aug 23, 2006 9:26 am
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.

Thanks

Posted: Wed Aug 23, 2006 10:19 am
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.