Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: form validation

by ZZamboni (Curate)
on May 10, 2000 at 06:13 UTC ( [id://10863]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://10863]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found