CGI::ValidOp::Check::number - CGI::ValidOp::Check module to check for numericity.


CGI-ValidOp documentation Contained in the CGI-ValidOp distribution.

Index


Code Index:

NAME

Top

CGI::ValidOp::Check::number - CGI::ValidOp::Check module to check for numericity.

DESCRIPTION

Top

default

Checks for something that looks like a number.

integer

Checks for an integer, positive or negative; includes 0.

decimal

Checks for a decimal, positive or negative; includes 0.

AUTHOR

Top

Randall Hansen <legless@cpan.org>

COPYRIGHT

Top


CGI-ValidOp documentation Contained in the CGI-ValidOp distribution.

package CGI::ValidOp::Check::number;
use strict;
use warnings;

use base qw/ CGI::ValidOp::Check /;

sub default {
    (
        qr/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/,
        '$label must be a number.',
    )
}

sub integer {
    (
        qr/^[+-]?\d+$/,
        '$label must be an integer.',
    )
}

sub decimal {
    (
        qr/^-?(?:\d+(?:\.\d*)?|\.\d+)$/,
        '$label must be a decimal number.',
    )
}

sub positive_int {
    (
        qr/^[+]?\d+$/,
        '$label must be a positive integer.',
    )
}

sub positive_list {
    my $self = shift;
    sub {
        my ( $input ) = @_;
        return $self->pass() unless defined $input;

        $input =~ m/(.*)/g;
        $input = $1;

        my @values = split(/\s*,\s*/, $input );
        my @bad;
        for my $value ( @values ) {
            next if $value =~ m/^[+]?\d+$/;
            push( @bad, $value );
        }
        if ( @bad ) {
            my $error = '$label: "' . join( ', ', @bad );
            $error .= (@bad > 1) ? '" are not positive integers.'
                                 : '" is not a positive integer.';
            return $self->fail( $error );
        }
        return $self->pass( join(', ', @values ));
    }
}

1;

__END__

# $Id: number.pm 75 2005-01-14 05:49:20Z soh $