Link to home
Create AccountLog in
Avatar of Cleavis
Cleavis

asked on

Input Validation Help

Hello,

I would like to ensure that the user input is numeric.  How can this be accomplished?

Thanks,

Cleavis
Avatar of ozo
ozo
Flag of United States of America image

It depends what you mean by numeric.
perldoc -q "How do I determine whether a scalar is a number/whole/integer/float?"
Found in /Library/Perl/pods/perlfaq4.pod
       How do I determine whether a scalar is a number/whole/integer/float?

               Assuming that you don't care about IEEE notations like "NaN" or
               "Infinity", you probably just want to use a regular expression.

                  if (/\D/)            { print "has nondigits\n" }
                  if (/^\d+$/)         { print "is a whole number\n" }
                  if (/^-?\d+$/)       { print "is an integer\n" }
                  if (/^[+-]?\d+$/)    { print "is a +/- integer\n" }
                  if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
                  if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal numbe
r\n" }
                  if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
                                       { print "a C float\n" }

               You can also use the Data::Types module on the CPAN, which
               exports functions that validate data types using these and
               other regular expressions.

               If you're on a POSIX system, Perl's supports the "POSIX::str-
               tod" function.  Its semantics are somewhat cumbersome, so
               here's a "getnum" wrapper function for more convenient access.
               This function takes a string and returns the number it found,
               or "undef" for input that isn't a C float.  The "is_numeric"
               function is a front end to "getnum" if you just want to say,
               ``Is this a float?''

                   sub getnum {
                       use POSIX qw(strtod);
                       my $str = shift;
                       $str =~ s/^\s+//;
                       $str =~ s/\s+$//;
                       $! = 0;
                       my($num, $unparsed) = strtod($str);
                       if (($str eq '') || ($unparsed != 0) || $!) {
                           return undef;
                       } else {
                           return $num;
                       }
                   }

                   sub is_numeric { defined getnum($_[0]) }

               Or you could check out the String::Scanf module on the CPAN
               instead. The POSIX module (part of the standard Perl distribu-
               tion) provides the "strtod" and "strtol" for converting strings
               to double and longs, respectively.
Avatar of Cleavis
Cleavis

ASKER

I want to ensure that the input contains only numbers 0-9

I do not want to allow any other characters including  a decimal point.

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
$input might contain a trailing \n if you read it as user input

chomp($input) ;
die "$input is not numeric\n" if $input ~ /\D/;

Ozo, for regular expressions, is "matching all characters to be numeric" faster or "matching even one character non-numeric" faster?? I believe the apporach given by me will be faster...is that true??

Manav

$input =~ /\D/; may be faster, but it will succeed for $input = ''; which I thought should be considered non-numeric.  
Although " I want to ensure that the input contains only numbers 0-9" seems to allow input containg no characters.