bradcathey has asked for the wisdom of the Perl Monks concerning the following question:
And for the module:use strict; require "Validate.pm"; use CGI qw(:standard); new CGI; my ($name, $phone, $email, $date, @errmsg, $errmsgs); $name = param('name'); &val_alpha($name, "Last Name", $errmsg[0]); $phone = param('phone'); &val_phone($phone, "Phone Number", $errmsg[1]); $email = param('email'); &val_email($email, "E-mail Address", $errmsg[2]); $date = param('date'); &val_date($date, "Birth Date", $errmsg[3]); &error_page(@errmsg); print "Errors:\n$errmsgs\n"; #handle errors sub error_page { for (@_) { if ($_) { $errmsgs .= "$_\n" } } } $template = HTML::Template->new(filename => "form.tmpl"); $template->param(errors => $errmsgs); #eventually process and write to the database __END__
#Validate.pm sub val_alpha { if ($_[0] =~ /^([A-Za-z -]*)$/) { $_[0] = $1; } else { $_[2] = "Invalid character(s) in $_[1]"; } } sub val_phone { if ($_[0] =~ /^[\(]?(\d{3})[\)\.\-]?(\d{3})[\)\.\-]?(\d{4})$/) { $_[0] = "$1-$2-$3"; } else { $_[2] = "Invalid $_[1]"; } } sub val_date { if ($_[0] =~ /^(\d{2})-(\d{2})-(\d{4})$/) { $_[0] = "$1-$2-$3"; } else { $_[2] = "Invalid $_[1]"; } } sub val_email { if ($_[0] =~ /^([\w\.\-]{3,})@([\w\.\-]{3,})\.([A-Z]{2,3})$/i) { $_[0] = "$1\@$2\.$3"; } else { $_[2] = "Invalid $_[1]"; } } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Am I passing and testing user data correctly?
by chromatic (Archbishop) on Nov 21, 2003 at 04:08 UTC | |
by bradcathey (Prior) on Nov 21, 2003 at 14:35 UTC | |
by chromatic (Archbishop) on Nov 21, 2003 at 18:17 UTC | |
|
Re: Am I passing and testing user data correctly?
by Zaxo (Archbishop) on Nov 21, 2003 at 04:00 UTC | |
by bradcathey (Prior) on Nov 21, 2003 at 04:07 UTC | |
by Zaxo (Archbishop) on Nov 21, 2003 at 04:18 UTC | |
|
Re: Am I passing and testing user data correctly?
by davido (Cardinal) on Nov 21, 2003 at 03:26 UTC |