skirrow has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I've been looking all through my perl book but can't find anything in it about this so have come here.

I need to check an input (string) is a. alphanumeric (a-z,A-Z,0-9,_,-,') and b. integer (0-9).

Any help with this is very much appreciated!
Thanks,
Neil

Replies are listed 'Best First'.
Re: Checking input
by tachyon (Chancellor) on Feb 25, 2003 at 20:40 UTC
    #!/usr/bin/perl -w use strict; use CGI; my $q = new CGI; # print a header print $q->header; # get param value using CGI ie <input type="text" name="param_name"> my $input = $q->param('param_name'); print "<h3>Test</h3>\n"; print "<p>'$input' is alphanumeric</p>" if $input =~ m/^[a-zA-Z0-9_'-] ++$/; print "<p>'$input' is integer</p>" if $imput =~ m/^\d+$/;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Thank you very much tachyon!
Re: Checking input
by Pardus (Pilgrim) on Feb 25, 2003 at 16:38 UTC
    perlre should do the trick
    quick answer is
    $input =~ /^\w+$/; $input =~ /^\d+$/;

    --
    Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
    >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<

      He also needs to check for '-', which \w doesn't do. That first regex should be /^[\w-]+$/

      ----
      Reinvent a rounder wheel.

      Note: All code is untested, unless otherwise stated

        It looks to me like the character class is the beginning of a range (the [\w-] bit). I know it isn't, and I know Perl has no problems with this (I just ran a test script to verify that).

        But wouldn't it be a little clearer to write the character class as either [-\w] or [\w\-] ? This is really just a formatting question more than anything else.

Re: Checking input
by Abigail-II (Bishop) on Feb 25, 2003 at 16:39 UTC
    Well, what did you try so far?

    Abigail

      I haven't tried anything yet, I couldn't find any information about how to. I couldn't get Pardus' suggestions to work. Returned internal server error even when using CGI::Carp qw(fatalsToBrowser). - Neil
        No idea at all? It looks like you haven't met one of the most fundamental cornerstones of the Perl language: regular expressions. Study the manual pages, or a book.

        And learn the language from the command line, don't try to learn it via CGI.

        Abigail

        A tip to reenforce [Abigail's sentiments:

        You can test your CGI scripts by running them from the command line before you deploy them. If the script needs command line parameters, supply them via the command line as well. If you pipe the output to a file, you can check the results by looking at the out with a browser. The best bit is that you will see any compiletime or runtime errors at the terminal rather than having to search the logs

        See the CGI.pm code for more info.


        ..and remember there are a lot of things monks are supposed to be but lazy is not one of them

        Examine what is said, not who speaks.
        1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
        2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
        3) Any sufficiently advanced technology is indistinguishable from magic.
        Arthur C. Clarke.
Re: Checking input
by Fletch (Bishop) on Feb 25, 2003 at 16:54 UTC

    You're in dire need of a better book if that's truly the case.