Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation: only letters/numbers 2

Status
Not open for further replies.

dreamstreet

Programmer
Jan 24, 2003
25
US


$username = param('username');

Hello,

How do i make sure that the user has entered only letters/numbers and not any illegal charters like @_)().

thanks

-a

d
 
Next time please psot what you have tried to solve your programming requirements, even if it did not work.

Only alpha and digits:

Code:
unless ($username =~ /^[a-zA-Z0-9]+$/) {
   print "You're a naughty boy!\n";
}

Only alpha, digits and underscore (same as \w):

Code:
if ($username =~ /\W/) {
   print "You're a naughty boy!\n";
}

\W is the compliment of \w, so anything that is not a-zA-Z0-9_ will return true and indicate illegal characters are present.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hello Kevin,

It does work but it seems to let the "()" go in. How can I block the something "(ss))))".

if ($uname =~/^[a-zA-Z0-9]+$/)) {
$error="User name can only contain letters or numbers.";
}

thank you

d
 
You did not pay close attention to the code I posted. Ignoring the syntax error in your code it would be like this:

Code:
$uname = '(ss))))';
[red]unless[/red] ($uname =~ /^[a-zA-Z0-9]+$/) {
   print "User name can only contain letters or numbers.";
}

or:

Code:
$uname = '(ss))))';
[red]if[/red] ($uname =~ /\W/) {
   print "User name can only contain letters or numbers and the underscore.";
}


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top