Alphanumeric usernames and error message for it

Check for alphanumeric

For more details, look at the Q on SO.

$name="input string 123";
// Doesn"t allow:
// Pre-/Appending white space
// Not more than one space in between
// Non-alphanumeric characters (lower case)
if( ! preg_match( "/^[a-z0-9]+([\\s]{1}[a-z0-9]|[a-z0-9])+$/i", $name ) )
{
    // Output Error Message
}
// Or: Does only allow
// Alphanumeric character (lower & upper case)
// Dash & underline "-", "_" - makes (imho) sense
if( ! preg_match( "/^[A-Za-z0-9-_",'\s]+$/", $name ) )
{
    // Output Error Message
}

Error Message

You need to do this via Ajax. Best would be to look over the search results. Another (maybe) good reference would be to inspect the code of some ajax login plugins for a “how-to”.