in reply to How to check CGI params in if/elsif

Err, just my .02, but I'd make it a little more readable, and instead of checking the e-mail regexp, get the user to enter their address twice, like the password. If they want to fake it they will, and double entry should pick up on typos (unless they're lazy like me and cut & paste :)

Note - I've assumed here that &error returns and 'email2' is the second email field.

my @required = qw(name site siteid email email2 pass repass); my $req_err; for (@required) { next if $q->param($_); $req_err++; last; } if ($req_err) { &error("You forgot some required fields"); } elsif ($q->param('email') ne $q->param('email2')) { &error("The two e-mail addresses you entered don't match"); } elsif($q->param('pass') ne $q->param('repass')) { &error("Your passwords don't match"); } else { # do whatever... }
As for site ID, if it's in a particular directory, first check it fits required format, then test - eg, if siteid is meant to be an integer:
$q->param('siteid') =~ /^\s*(\d+)\s*$/ my $site_id = $1 if ($site_id) { if (-d '/path/to/sites/'.$site_id) { &error("Directory already exists"); } else { # directory doesn't exist, so do whatever... } } else { &error("Invalid Site ID"); }
If &error doesn't return, you can simplify further:
my @required = qw(name site siteid email email2 pass repass); for (@required) { next if $q->param($_); &error("You forgot some required fields"); } $q->param('email') eq $q->param('email2') or &error("The two e-mail addresses you entered don't match"); $q->param('pass') eq $q->param('repass') or &error("Your passwords don't match"); my ($site_id) = $q->param('siteid') =~ /^\s*(\d+)\s*$/; $site_id or &error("Invalid Site ID"); -d '/path/to/sites/'.$site_id or &error("Directory already exists"); # directory doesn't exist, so do whatever...
.02

cLive ;-)

--
seek(JOB,$$LA,0);

Replies are listed 'Best First'.
Re: Re: How to check CGI params in if/elsif
by Anonymous Monk on Jun 11, 2002 at 11:20 UTC
    my @required = qw(name site siteid email email2 pass repass); my ($site_id) = $q->param('siteid'); my $req_err; for (@required) { next if $q->param($_); $req_err++; last; } if ($req_err) { &error("You forgot some required fields"); } elsif ($q->param('email') ne $q->param('email2')) { &error("The two e-mail addresses you entered don't match"); } elsif($q->param('pass') ne $q->param('repass')) { &error("Your passwords don't match"); } elsif (-d "/var/www/virtual/webewebin.com/home/$site_id") { &error("Directory already exists"); } else { }

    That worked just fine, if I would of done this
    $q->param('siteid') =~ /^\s*(\d+)\s*$/;

    Then it says everysite id exists, if I would of just did
    my ($site_id) = $q->param('siteid');

    It would work fine