Validating script (php) doesn't allow spaces in username field.

Attente
Attente used Ask the Experts™
on
Hi all gurus,
this is the first time I use EE, since I'm a complete newbie in the PHP area and learn it just for fun.
Here's my question: the Username field is being validated by a php ereg function.
if (!ereg("^[a-zA-Z'0-9_-]+$", $username)) {
echo "Incorrect characters in Username field.<br>";
}
However, it does not allow the use of spaces in Username field, e.g. "John Smith" will get an error and will not be processed to  the databse. So is there a way to allow using spaces in Username field? And - if it doesn't look too brazen on my part - to control them so that two and more spaces do not occur one by one, so that "John Smith" would pass validation and "John  Smith would get an error message"?
Thanks to all for your help in advance!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
try:

if (!ereg("^[a-zA-Z'0-9_-]+ ?[a-zA-Z'0-9_-]+$", $username)) {
echo "Incorrect characters in Username field.<br>";
}

Commented:
Hmm... I don't know about the two spaces, but you're not that far off:

$username = trim($username);
if (!ereg("^[a-zA-Z'0-9_- ]+$", $username))
{
  //error
}

This will allow you to have spaces in your username

If you do a trim() (as shown above), it will get the extra spaces off the front and back... but as for in the middle, I don't know what to tell you.
Commented:
Actually, I do have an idea.

if (!ereg("^[a-zA-Z'0-9_- ]+$", $username) AND ! strstr($username, "  "))
{
 //error
}

Author

Commented:
Thank you very much why2liz and duerra for your help!
So far I've implemented the code:
if(!ereg("^[a-zA-Z'0-9_-]+$", $mielofonec) && !strstr($mielofonec," ")) {
//error
}
Now user's input with spaces IS validated. The data is put into mysql database. Now comes the Log In form which validates the username against all the inputs in the database. If say "John Smith" was registered with 2 spaces in the middle, than the accepted login should contain 2 spaces in the middle as well, otherwise the Username will not be recognized as the existing in the database. So I guess I'm stuck with this since to limit the number of allowed spaces is virtually imposible.. it simly goes beyond my poor knowledge of php. Probably I'll make some note on this point on the Registration Page so that users were aware of the "multi-spaces" problem and avoided their multiple instances.
Anyway, thanks for your input, 'cause I couldn't find anywhere the information on how to allow spaces. Usually people ask of how to prevent users from entering spaces, you know :)
Sincerely yours,
Attente.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial