in reply to form validation

(Next time, remember to use the <code> tag to enclose your code)

It should work if you remove the negation. Right now it says "if the username does not contain a non-alphanumeric character, generate an error", which is exactly the opposite of what you want. Remove the "!" and it should be corrected.

Also, remember that \w in Perl regular expressions represents the "alphanumeric plus underscore" class, exactly the class you used, and \W represents its complement. So you could do the test like this:

if ($FORM{username} =~ /\W/) { &error; } else { &proceed; }
IMHO it is clearer and safer to check that the string contains only valid characters, instead of checking if it contains invalid ones. Like this:
if ($FORM{username} =~ /^\w+$/) { &proceed; } else { &error; }

--ZZamboni

Replies are listed 'Best First'.
RE: Re: form validation
by perlmonkey (Hermit) on May 10, 2000 at 06:25 UTC
    IMHO it is clearer and safer to check that the string contains only valid characters, instead of checking if it contains invalid ones.

    I disagree that it is safer, I find the one with less characters easier to read. Also your prefered method is slower. It is easier to find one bad apple than to look through and validate every one. The net result maybe the same but only in the 'worst case' senario. But that is only IHMO also.