CGI Paramater Validation CPAN Modules 19 of 32 : [PREV] [NEXT]

CGI::Validate

use CGI::Validate qw(:standard);  # Normal use

my $FieldOne    = 'Default String';
my $FieldTwo    = 8;
my $FieldThree  = 'some default string';
my @FieldFour   = ();  ## For multi-select field
my @FieldFive   = ();  ## Ditto
my $EmailAddress= '';

## Try...
my $Query = GetFormData (
    'FieldOne=s'  => \$FieldOne,     ## Required string
    'FieldTwo=i'  => \$FieldTwo,     ## Required int
    'FieldThree'  => \$FieldThree,   ## Auto converted to the ":s" type
    'FieldFour=s' => \@FieldFour,    ## Multi-select field of strings
    'FieldFive=f' => \@FieldFive,    ## Multi-select field of floats
    'Email=e'     => \$EmailAddress, ## Must 'look' like an email address

## Catch...
) or do {
    if (%Missing) {
        die "Missing form elements: " . join (' ', keys %Missing);
    } elsif (%Invalid) {
        die "Invalid form elements: " . join (' ', keys %Invalid);
    } elsif (%Blank) {
        die "Blank form elements: " . join (' ', keys %Blank);
    } elsif (%InvalidType) {
        die "Invalid data types for fields: " . join (' ', keys %InvalidType);
    } else {
        die "GetFormData() exception: $CGI::Validate::Error";
    }
};

the '=swifex' data type following the parameter name allow for some builtin data checking, these are

## Builtin data type checks available are:
s    string     # Any non-zero length value
w    word       # Must have at least one \w char
i    integer    # Integer value
f    float      # Float value
e    email      # Must match m/^\s*<?[^@<>]+@[^@.<>]+(?:\.[^@.<>]+)+>?\s*$/
x    extension  # User extension type.  See EXTENSIONS below.

© 2003 Barbie barbie@missbarbell.co.uk Home http://birmingham.pm.org/