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!

Alpha-Numeric

Status
Not open for further replies.

bnc123

Technical User
Mar 25, 2001
59
AU
How do I verify that a string entered from an on-line form (say $name) consists only of a combination of alphabets and numbers (alpha-numeric), and does not have spaces or symbols.

There is a perl snippet for doing this, but it completely escape my mind.

Can someone please refresh my memory?

Thanks.
 
Try:
Code:
if ($myString =~ m/^\w+$/i) {
   #IF THE STRING IS ALPHA-NUMERIC
} else {
   #IF THE STRING IS NOT ALPHA-NUMERIC
}
(this consider the character '_' being alphanumeric).

If you don't want this character:
Code:
if ($myString =~ m/^[a-zA-Z0-9]+$/i) {
   #IF THE STRING IS ALPHA-NUMERIC
} else {
   #IF THE STRING IS NOT ALPHA-NUMERIC
}

Hope it helps.
 
My personal opinion is to not do form verrification only with cgi. With CGI the form has to submit to the server then the user gets the next page that says their mistakes. It is better to do simple form verrification with javascript, which will not let the form submit if the values are not alpha-numeric. You can always have both methods, and while server side is the most secure, it can annoy many people with the numerous form submissions.
 
Many thanks naq2 and rob51383. The input from both of you have helped me a lot.

I have tried it out and it works. I will use javascript and perl both. If I use javascript alone, people can fiddle with the page source and then try all sorts of pranks.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top