/    Sign up×
Community /Pin to ProfileBookmark

Form Validation…again.

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 ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 24.2005 — [code=php]
$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.
}
[/code]
Copy linkTweet thisAlerts:
@tgrk35authorOct 24.2005 — Thanks, man.

That looks simple enough ?.
Copy linkTweet thisAlerts:
@tgrk35authorJan 27.2006 — [code=php]
$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.
}
[/code]
[/QUOTE]


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
Copy linkTweet thisAlerts:
@SpectreReturnsJan 27.2006 — But that... that isn't what you said! ?
Copy linkTweet thisAlerts:
@tgrk35authorJan 27.2006 — ...because that's not what I needed previously... Now I need a script that'll validate only certain <input>'s
Copy linkTweet thisAlerts:
@NogDogJan 28.2006 — Some people do it by using a naming convention for all form fields to indicate if it's a required field:
<i>
</i>&lt;form action="handler.php" method="post"&gt;
Field 1: &lt;input type="text" name="REQ_field1"&gt; &lt;!-- required --&gt;
Field 2: &lt;input type="text" name="OPT_field2"&gt; &lt;!-- optional --&gt;
&lt;/form&gt;

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.
×

Success!

Help @tgrk35 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...