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 only

Status
Not open for further replies.

lowtide

Programmer
May 17, 1999
24
US
I am trying to check a string to make sure it contains only alpha-numeric characters. I have tried the following, but it does not work:

if (!eregi("[a-z0-9]",$request)){ .....

(error code)......

What am I doing wrong. Shouldn't this tell me if there are nonalpha-numeric characters in the $request string and if so then go through the code to produce an error message.

 
I think the only problem is that you're not looking for the uppercase letters. If so, try this:

[tt]if (eregi("^[a-zA-Z0-9]", $request))
{
//error code
}[/tt]
 
Thanks kapok,
The ^ was what I needed.
One more question.
How would I also include "_"'s and "-"'s ?

 
It's simple, just add the characters you need to match:

[tt]eregi("^[a-zA-Z0-9_\-]", $request)[/tt]

Make sure you escape the dash(-) with a backslash(\). You also have to escape the period(.), just for future reference.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top