The second RegEx contains two spaces (not one), so what it does is remove extra spaces but one to nothing.

Huh? If I understand your statement, then you might not understand the process...

$data =~ s/\s+/ /g; # Remove extra spaces to nothing $data =~ s/ +//g;
Regardless of what sort of whitespace is contained in $data, your first regex eliminates all cases where two or more whitespace characters are adjacent -- it changes every string of one or more whitespace to a single space -- which means there will be nothing left for your second regex to apply to, so the second one is not needed.

(And if you were to apply the second regex to a string that happened to contain two or more consecutive spaces -- i.e. where the first regex had not been applied -- it would delete all the spaces in that sequence, not leaving any behind.)

As for the rest, I second the earlier suggestions to look at established modules for this sort of thing, especially if you expect there will be an increasing need for it.

Also, just as a matter of style, it's often nicer to have the shorter, simpler, and/or more primary conditions fully handled first, and deeper, more complicated ones later. In general, this allows for simpler code, with less nesting of "if ... else ..." conditions; eg:

$length = length( $data ); if ( ! $length ) { # empty string if ( $obligatory ) { bail_out( "Obligatory field not filled in" ); } else { return; } } if ( $data =~ /([^-\@\w. ])/) { bail_out( "Bad character: $1 -- Only alphanumerics and -@. allowed" + ); } if ( $numeric and $data =~ /[^\d]/ ) { bail_out( "Non-numeric character(s) in numeric field" ); } if ( $min and $length < $min ) { bail_out( "too short" ); } if ( $max and $length > $max ) { bail_out( "too long" ); } return $data;
I haven't tested it, but I think that covers everything you posted. Instead of being a long block of nested logic, it's a set of simple, discrete checks, arranged in such a way that the later ones only need to be checked if the earlier ones pass. Note that the case of $min==$max does not need special treatment, and the checks for acceptable character content and for digits involve seeing if any unacceptable characters are present.

(Is it possible that negative and/or decimal-fraction numbers should be allowed in some numeric fields? If so, that condition should be:  $data =~ /[^\d.-]/)

(update: fixed the code slightly, to give a better error message in the character-class check. Did some rewording.)


In reply to Re: Re: Re: Check input sub - comments please... by graff
in thread Check input sub - comments please... by kiat

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.