The apprentice, after taking aspirin for the pain in his forehead, finally caves after several hours...

Some time ago, a certain monk (privately) advised the following after seeing my poor attempts to verify that a certain CGI parameter was actually a number, e.g. a non-blank, Base-10 numeric value that may (or may not) contain decimal points and/or hyphens masquerading as negative number indicators:

Best perlop check? Try this: eval {local $^W = 1; $num + 0}; # Now check $@

Okay. So I'm trying to suit the action to these words and am not having much success:

#!/usr/bin/perl -wT use strict; $|++; use CGI qw( :standard ); my $result = ''; my %params = param(); if ( my $error = cgi_error() ) { print header( -status => $error ); exit 0; } my $invalue = param( 'INVALUE' ); if ( defined $invalue ) { eval{ local $^W = 1; $invalue + 0 }; ### Is Number? (Line 19) if ( $@ ) { $result = "$invalue is *not* a Number; Details: $@" } else { $result = "$invalue appears to be a number."; } } else { $result = "Enter a value to test." } print header( "text/html" ), start_html('Test'), h1('Validation'), start_form(), p( $result ), p( "Please enter a number to test: ", textfield( 'INVALUE', '' ) + ), p, submit, end_form, hr,"\n"; print end_html; exit 1;

As the more experienced of you will recognize, this is resulting in values such as "20" as being recognized as a number. Sadly, so are "Shoe", "Fred" and other patently non-numeric values, by which (just to be perfectly clear to the most rententive) are *not* numeric in the locale, idiom, and operational parameters that I'm trying to communicate in.

My petitions are:

Yes, I am aware of the Cookbook and its examples; you'll note this monk had a different approach in mind. I'm trying to get it to work. A gentle nudge would be greatly appreciated.

Many thanks in advance...

--f


In reply to Validating Numbers in CGI Script? by footpad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.