use preg_match preg_match ( string pattern, string subject [, array &matches [, int flags [, int offset]]] )
The pattern you need is /^[a-z0-9-]+$/i or you can do the opposite: if (!preg_match("/[^a-z0-9-]/", ...