Re: Validating numeric input
by davido (Cardinal) on Feb 05, 2005 at 05:56 UTC
|
It's a little more complicated than that to validate numbers. That regex doesn't allow negative numbers, nor does it allow decimal values. It only accepts positive integers.
For authenticating a single number, it's pretty easy to use Scalar::Util's looks_like_number() function.
You could split on whitespace to isolate two entities, and then test each one to see that it looks like a number.
| [reply] [d/l] |
|
|
I noticed. I am able to select for positive integers, but thats about it! When I try to do something like its just no go! This is probably fraught with mistakes, going from memory, but I'm sure you get the point
Bang line
$value1=shift
$value2=shift
unless (($value1 >= 0) or ($value1 <= 0) or ($value2 >= 0) or ($value2 <= 0)){
die;
$sum "blah blah";
print "blah blah sum";
| [reply] |
Re: Validating numeric input
by superfrink (Curate) on Feb 05, 2005 at 07:46 UTC
|
When I am expecting an integer I often just cast it with the perlfunc:int keyword like this:
my $line = <>;
chomp $line;
my $num = int $line;
Now of course that maps input like "asdf" to the integer zero so you still need to check the input like you are doing. There are some related comments in the perlfaq at How do I determine whether a scalar is a number/whole/integer/float? | [reply] [d/l] |
Re: Validating numeric input
by K_M_McMahon (Hermit) on Feb 05, 2005 at 06:20 UTC
|
If you are using values entered from <STDIN> don't forget to chomp($value) the value. This will remove a newline character (if it exists). A newline character at the end of $value in your example would cause it to fail the regex as well as the above noted problems.
| [reply] [d/l] [select] |
|
|
lemur:~ 731> perl -le '$_ = "1234\n"; print "You sure?" if /^\d+$/'
You sure?
lemur:~ 732>
| [reply] [d/l] |
Re: Validating numeric input
by cognizant (Pilgrim) on Feb 05, 2005 at 06:25 UTC
|
Hi,
Below code validates for exact 2 numbers.
if ($value !~ /^\d{2}$/)
{
print "input contains more than two digit";
}
If input contains negative number or so. Then
if ($value !~ /^(-)*\d{2}$/)
{
print "input contains more than two digit";
}
cheers,
--c | [reply] [d/l] [select] |
|
|
No, that code checks for a string of exactly two digits, not two numbers (Your print is correct, your text statement is not.)
It also fails for numbers in hex and numbers in scientific notation.
| [reply] [d/l] |
Re: Validating numeric input
by cog (Parson) on Feb 05, 2005 at 22:49 UTC
|
unless ($value =~ /^\d+$/){ "$value contains a letter. Numbers don't have letters in them!!!\n"; die; }
Actually, just because a variable has something in it that does not match \d doesn't mean it is a letter... | [reply] [d/l] |
Re: Validating numeric input
by zentara (Cardinal) on Feb 06, 2005 at 11:08 UTC
|
A good bet for you would be Regexp::Common::number
I'm not really a human, but I play one on earth.
flash japh
| [reply] |
Re: Validating numeric input
by m-rau (Scribe) on Feb 06, 2005 at 11:33 UTC
|
There is a very good chapter "NUMBERS" in the Perl Cookbook by Tom Christiansen & Nathan Torkington. This book is a very good starting point for Perl newbies if they are are no programming beginners.
Here are some precooked solutions (the cookbook's equivalent of just-a
+dd-water meals) for most common cases.
warn "has nondigits" if /\D/;
warn "not a natural number" unless /^\d+$/; # rejects -3
warn "not an integer" unless /^-?\d+$/; # rejects +3
warn "not an integer" unless /^[+-]?\d+$/;
warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2
warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/;
warn "not a C float"
unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
Michael. | [reply] [d/l] |
Re: Validating numeric input
by Anonymous Monk on Feb 07, 2005 at 09:50 UTC
|
Well, what kind of numbers do you want to accept? Is 0.123 allowed? What about .123? -123? +123? Do you allow 1,234? And if you do, is the comma used to separate thousands, or is it a European style decimal point? Do you allow 0x123? What about floats? -1.23E-45? Perl allows underscores in certain cases, like 1_234, do you? If you do, then Perl doesn't like 12_34, what about your program?
Perhaps the regexes in the FAQ are use of you. Perhaps Scalar::Util::looks_like_number. Perhaps Regexp::Common. Perhaps you want something that isn't covered by those three. | [reply] |