I've only just got into PHP myself but I have a pile of these suckers in my first project, so here goes ...
If that's all you want to check for and there's nothing weird (like how a user has typed in an e-mail address) then it's fairly simple. Try this ...
if (!eregi("[a-zA-Z0-9_-]+$",$yourfieldname)
{
... blah blah
}
I'll try to break this down for you. A couple of bits threw me at first but it's surprisingly understandable once you know how to read it.
The '' tells the system to start checking from the first character in the string you want to check (in this case, $yourfieldname).
Inside the square brackets you have the characters you want to check for in the string. These are the characters the user has to type for the contents of the string to be valid. So here, we want what you have asked for ...
a-z = all lowercase letters
A-Z = all uppercase letters
0-9 = all numbers
_ = the underscore
- = the dash.
The \ is used to tell the system that the next character is a special character. For some characters(such as ", I imagine) your code will be incorrectly interpreted without it. I don't know if you need it for a '-' but it shouldn't do any harm to include it.
The '+' tells the system to check one or more times and the '$' basically says until the end of the string has been reached.
Hope I got that right.🙂