Forum Moderators: coopster & phranque

Message Too Old, No Replies

regex an array

         

idiotgirl

7:43 am on Jan 20, 2002 (gmt 0)

10+ Year Member Top Contributors Of The Month



Been regexing 'til the cows come home on individual form values to validate user input - and then it dawned one me - why not regex a whole array of form values that need similar regex-ing and return an error message for each one that doesn't meet the regex criteria?

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?

Brett_Tabke

8:26 am on Jan 20, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You could use Grep.

@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;

idiotgirl

8:46 am on Jan 20, 2002 (gmt 0)

10+ Year Member Top Contributors Of The Month



Well - I want the error messages written to the user while they are filling out the form and hitting submit, rather than relying on regex to fix it for me after the data is input. I've come into g-r-e-a-t problems with users not reading the instructions of what to put into the form fields.

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.

Brett_Tabke

9:57 am on Jan 20, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Ok, I think I understand what you are looking for:

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;
}

idiotgirl

10:44 am on Jan 20, 2002 (gmt 0)

10+ Year Member Top Contributors Of The Month



hey! while I was away here for the last little bit - I fixed it. Came back to see Brett's post in my Christmas stocking.

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.