Is there a quick way to check if a scalar contains non-alpha-numeric characters?

J

John Smith

Hi all,

I want to ensure that a variable does not contain spaces, and other
non-alpha-numeric characters such as !#$%^&*()-=+, etc... Basically, this
variable is a username that someone is sending to me and will only be 4 to
10 characters long. It must only contain numbers and letters, otherwise I
will reject it.

Is there a quick way to do this or must I create a loop and check every
letter?

Thanks for all,

Guy Doucet
 
A

Anno Siegel

John Smith said:
Hi all,

I want to ensure that a variable does not contain spaces, and other
non-alpha-numeric characters such as !#$%^&*()-=+, etc... Basically, this
variable is a username that someone is sending to me and will only be 4 to
10 characters long. It must only contain numbers and letters, otherwise I
will reject it.

Is there a quick way to do this or must I create a loop and check every
letter?

No. A regular expression (or a tr///) and a call to length() should do.

What have you tried so far?

We are happy to help with code, but not happy to write solutions from
scratch.

Anno
 
G

Gunnar Hjalmarsson

John said:
I want to ensure that a variable does not contain spaces, and other
non-alpha-numeric characters such as !#$%^&*()-=+, etc...
Basically, this variable is a username that someone is sending to
me and will only be 4 to 10 characters long. It must only contain
numbers and letters, otherwise I will reject it.

Is there a quick way to do this or must I create a loop and check
every letter?

That is easily done with a regular expression.

perldoc perlre

http://www.perldoc.com/perl5.8.0/pod/perlre.html
 
G

Glenn Jackman

John Smith said:
I want to ensure that a variable does not contain spaces, and other
non-alpha-numeric characters such as !#$%^&*()-=+, etc... Basically, this
variable is a username that someone is sending to me and will only be 4 to
10 characters long. It must only contain numbers and letters, otherwise I
will reject it.

reject($username) unless $username =~ /^[A-Za-z\d]{4,10}$/;
 
D

David K. Wall

Glenn Jackman said:
John Smith said:
I want to ensure that a variable does not contain spaces, and
other non-alpha-numeric characters such as !#$%^&*()-=+, etc...
Basically, this variable is a username that someone is sending
to me and will only be 4 to 10 characters long. It must only
contain numbers and letters, otherwise I will reject it.

reject($username) unless $username =~ /^[A-Za-z\d]{4,10}$/;

I think you mean /^[A-Za-z0-9]{4,10}$/ or maybe /^[[:alnum:]]{4,10}$/

Putting \d inside a character class matches '\' and 'd', not digits.

I'll let someone else worry about locale, although I suspect it doesn't
matter much for the OP.
 
J

Jeff 'japhy' Pinyan

Glenn Jackman said:
reject($username) unless $username =~ /^[A-Za-z\d]{4,10}$/;

Putting \d inside a character class matches '\' and 'd', not digits.

Not true. The pseudo-classes \w, \W, \s, \S, \d, and \D work fine in a
character class.
 
U

Uri Guttman

DKW> Putting \d inside a character class matches '\' and 'd', not digits.

perl -le 'print "match" if "123" =~ /[\d]/'
match

you can put any char class shortcut (\d,\s,\D, etc.) into a char
class. it just is merged (union in set terms) with all the other chars
and shortcuts in that char class.

uri
 
G

Glenn Jackman

David K. Wall said:
Glenn Jackman said:
reject($username) unless $username =~ /^[A-Za-z\d]{4,10}$/;
I think you mean /^[A-Za-z0-9]{4,10}$/ or maybe /^[[:alnum:]]{4,10}$/

Ah, the use of the [:alnum:] class would be the best solution, as it is
probably resistant to changes in locale or use of unicode.
 
D

David K. Wall

Jeff 'japhy' Pinyan said:
Glenn Jackman said:
reject($username) unless $username =~ /^[A-Za-z\d]{4,10}$/;

Putting \d inside a character class matches '\' and 'd', not
digits.

Not true. The pseudo-classes \w, \W, \s, \S, \d, and \D work fine
in a character class.

Sheesh, my batting average is sucking. And I even tested it. (Note to
self: SLOW DOWN)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top