If you want to check for real numbers, try something like the demo script
below. '-12.3' shows true while '--12.3' shows false.
HTH
Gary
#!/usr/bin/perl
my $fred='-12.3';
$fred+=1;
$fred-=1;
$resp= ($fred) ? "$fred is numeric\n" : "$fred is not numeric\n";
print $resp;
On Thursday 27 Feb 2003 12:47 am, Casey West wrote:
> It was Wednesday, February 26, 2003 when T. Murlidharan Nair took the soap
box, saying:
> : I have a cgi that need to accept only numeric values. ie +ve or -ve
> : real numbers.
> : Is there a quick and easy way to check this. I was trying using a reg exp
> : if(/^[-0-9][\.0-9]*/) {
> : do something
> : }
> :
> : but this breaks when the number is say --75.4 It still accepts if it
> : has two - signs.
>
> Here's a nice trick. Use the int() function. It's documented in
> perlfunc, a short to the documentation is 'perldoc -f int'.
>
> When passed a string, int() will return 0. When passed a number, it
> will return the integer version of that number. Armed with this
> knowledge, it's fair to assume that when passed an integer, the
> following expression will be true:
>
> $data == int( $data );
>
> It will return false if a fraction or a string is passed, in '$data'.
>
> So, try this:
>
> if ( $_ == int( $_ ) ) {
> # do something...
> }
>
> Enjoy!
>
> Casey West
--
Gary Stainburn
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]