The apprentice, after again struggling for quite some time, caves...

Consider, if you will:

#! /usr/bin/perl -wT use strict; use CGI qw( :standard ); my $testvalue = ''; printForm(); if ( param() ) { br(); $testvalue = param( 'TestValue' ); my $errors = ""; $errors .= paramCheck( "Test Value", $testvalue, 1, 30, '/\W/', "letters and numbers" ); if ( $errors ) { print p( "Failed" ), "<UL>$errors</UL>" } else { print p( "Passed...wow, what a surprise." ); } } exit 1; sub paramCheck # Returns "" if no error or description of problem(s). { my $lbl = shift; # Field name for error messages my $val = shift; # Value received from the CGI query my $min = shift; # Minimun length; Use >0 for required fields my $max = shift; # Maximum allowed length my $pat = shift; # Regex to validate the data against. my $msg = shift; # Explanation added to error if Regex fails. my $err = ""; # Holds error message my $len = length( $val ); if ( $min ) # <> 0 { if ( $len < $min ) { $err .= wes( "$lbl cannot be blank." ); } } elsif ( $len > $max ) { my $s = $len == 1 ? "" : "s"; $err .= wes( "$lbl is currently $len character$s; " . "; it can only be $max character$s long." ); } else { unless ( $val =~ $pat ) { $err .= wes( "$lbl contains invalid characters; " . "it can only hold $msg." ); } } return $err; } sub printForm { print header(), start_html( -title => 'Test' ); print start_form( "post", "/cgi-bin/valtest.cgi", "application/x-www-form-urlencoded" ); print p( 'Enter a value:' ), textfield( 'TestValue', $testvalue, '40', '30' ); print br(), submit( "Try it" ), '&nbsp;&nbsp;', reset( "Reset" ); print end_form(); print end_html(); } sub wes # Wrap an Error String with desired tags. { my $input = shift; return "<LI>" . $input . "</LI>"; }

The idea, of course, is to devise a routine that handles the bulk of my CGI parameter validation. Unfortunately, I'm not seeing the results I expect with the regular expression in this example. Specifically, it always claims to match, even though I submit values such as "~", "/", "\", and so on (characters I'm specifically trying not to accept--hence the \W.)

My petitions are:

--f

P.S. Possibly relevant details: Perl v5.005_03, CGI v2.753, BSD 4.2.


In reply to Dynamic Regex? 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.