Gossamer Forum
Home : General : Perl Programming :

Error Checking.....

Quote Reply
Error Checking.....
Yuck....I've been struggling to get error checking to work for a form processing script I am working on. I know I'm overlooking something really stupid but I just can't find it.

I have about 8 field like....

$name = $FORM{name};
$email = $FORM{email};

I would like all the errors listed at once on one page. I have looked at how links does it and tried to modify the code to work for me but can't get it to work - help pleeaaaaaaase :)

This is what I have.....

@required = qw($name $email $address $phone $zip);

foreach (@required) {
if ($_ =~ /^\s*$/) {
push(@bad,"$_ cant be left blank");
}

if ($#bad 1 > 0) {
foreach $error (@bad) {
$errstr .= "<li>$error";
}
&error($errstr);
}

Then in sub error I have...

my ($errstr) = shift;

Shouldn't this work???

Thankyou.

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Error Checking..... In reply to
In your code:

if ($#bad 1 > 0) {
foreach $error (@bad)
$errstr .= "<li>$error";
}
&error($errstr);
}

Do this instead:

if ($#bad > -1) {
foreach $error (@bad)
$errstr .= "<li>$error";
}
&error($errstr);
}


If @bad is empty, it will, in fact, be equal to -1.


Quote Reply
Re: Error Checking..... In reply to
Ugh there was a plus sign in my code but the forum removes it.....

Code:
if ($#bad 1 > 0) {
It still doesn't work

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Error Checking..... In reply to
I'm sorry, you left off the { after your "foreach $error (@bad)":

if ($#bad > -1) {
foreach $error (@bad) {
$errstr .= "<li>$error";
}
&error($errstr);
}




Quote Reply
Re: Error Checking..... In reply to
Ack sorry....hands moving quicker than my brain Smile

I eventually got it working using a demo.....

#!/usr/bin/perl

print "Content-type: text/html\n\n";

$a = 1;
$b = 2;
$c = 3;
$d = 4;

push(@errors, "a can't be zero") unless $a = 0;
push(@errors, "I said a cant be zero") unless $a = 0;
push(@errors, "Are you nuts?") unless $a = 0;
push(@errors, "Oh go away...") unless $a = 0;

if ($#errors > -1) {

foreach (@errors) {
$new .= "<li>$_";
}

}
print $new;


Thanks for the reply.


Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/