Click to See Complete Forum and Search --> : Form Validation...again.


tgrk35
10-24-2005, 01:03 AM
I know this has been covered like 9000 times in the forums, but I'm illiterate and most of the scripts I saw were damned complicated.

I just need a SIMPLE validation script in PHP to validate ALL fields in my form. I'm not really too concerned about CONTENT as I am BOOLEAN (as long as there's something in the box, I'm happy).

Anyone think they can help me out? Just need something to say "is there something in every box? yes? ok, then process this stuff, if not, echo a message saying "get yourself together...or I'll kill you."

(you don't have to make it say that, btw ;))

Thanks, guys :)

NogDog
10-24-2005, 02:02 AM
$ok = TRUE;
foreach($_POST as $value)
{
if(trim($value) === "")
{
$ok = FALSE;
break;
}
}
if(!$ok)
{
echo "<p>get yourself together...or I'll kill you.</p>\n";
}
else
{
# proceed with handling the form data.
}

tgrk35
10-24-2005, 02:16 AM
Thanks, man.

That looks simple enough :D.

tgrk35
01-27-2006, 02:20 PM
$ok = TRUE;
foreach($_POST as $value)
{
if(trim($value) === "")
{
$ok = FALSE;
break;
}
}
if(!$ok)
{
echo "<p>get yourself together...or I'll kill you.</p>\n";
}
else
{
# proceed with handling the form data.
}


Is there a way to modify this so that it only validates certain fields? I have a form where not EVERY field needs to be filled out, just some.

Thanks so much,
Will

SpectreReturns
01-27-2006, 04:32 PM
But that... that isn't what you said! :(

tgrk35
01-27-2006, 04:34 PM
...because that's not what I needed previously... Now I need a script that'll validate only certain <input>'s

NogDog
01-27-2006, 07:55 PM
Some people do it by using a naming convention for all form fields to indicate if it's a required field:

<form action="handler.php" method="post">
Field 1: <input type="text" name="REQ_field1"> <!-- required -->
Field 2: <input type="text" name="OPT_field2"> <!-- optional -->
</form>

Then in your validation function, you would check each field name to see if it begins with "REQ_" and only require that it be non-empty if it does.