Then the brilliant idea turned to mud.
@formstuff = ("$form{'this_field'}","$form{'that_field'}","$form{'more_stuff'}",); foreach $thing(@formstuff) {
if ($form{$thing} = <gets regexed>) {
print "your form input for $form{'thing'} sucks - clean it up";
}
}
Okay - so that's the real sloppy version and - of course - not even correct. I know this. But I've been dinking with this, that, and the other thing and I can't get each item of the array to be regexed and return the form value's name/value for an error message.
This is simple. Isn't it?
@stuff = grep(/regexhere/,@stuff);
Now you are left with only what you want in @stuff. Multiple times faster than looping through it with a regex.
If you want to keep "stuff" as it starts:
@newstuff = grep(/regexhere/,@stuff);
If you are looking for form errors, here is what I do with post affixed if/thens to build up error messages:
$error ="";
$error .="<li><b><i>dotcom</i> not allowed in user names</b>" if ($inusername =~ /dot\_com/gi);
$error .="<li><b><i>dotcom</i> not allowed in user names</b>" if ($inusername =~ /dotcom/gi);
$error .="<li><b><i>dotcom</i> not allowed in user names</b>" if ($inusername =~ /dot com/gi);
$error .="<li><b><i>dotnet</i> not allowed in user names</b>" if ($inusername =~ /dot\_net/gi);
$error .="<li><b><i>dotorg</i> not allowed in user names</b>" if ($inusername =~ /dotorg/gi);
&GoError if $error;
This is why I wanted to do a foreach, that would identify which form field(s) they were currently assaulting.
If I can regex each item of the array after the submit button is pressed, and before their awful data contaminates my db, which is read in as
$form{'fieldname_this'}, $form{'fieldname_that'}, etc. forever I believe I can kill two birds with one stone: 1.) educate/chastise the ab/user 2.) prevent crap data filling my db.
sub TestForm {
$snack = $FORM{'snack'}; #data values _in_ from the user.
$email = $FORM{'email'};
$name = $FORM{'name'};
$error ="";
$error .="<li><b>sorry we don't allow cream puffs - only donuts</b>" if ($snack =~ /cream puffs/gi);
$error .="<li><b>oops email doesn't appear valid" if ($email !~ /\@/gi);
$error .="<li><b>oops email doesn't appear valid" if (length($email) <3);
$error .="<li><b>We need a name</b>" if (!$name);
&GoError if $error;
...process valid form here....
}
sub GoError {
if ($error) {
print "<b>The following errors occurred during your registration:</b><p>\n";
print "<ul>$error\n";
print "</ul><p>\n";
print "Please use your back button to correct this information.</body></html>\n";
}
exit;
}
Left all the form{'blah'} stuff in an array - did a foreach - and each error message is stored in a list - then prints with the GoError I just picked up here.
I love it when a plan comes together. Thanks, Brett.