Function to check username without spaces

genesisvh used Ask the Experts™
Ok so I'm still having problems. This is what I need:
I want a function to echo a message if a username does not pass my requirments.

  - Only letters and / or numbers
 - Must start with a letter (not a number)
 - No spaces

The current code is not working for me. Please help
}else if (!(eregi('/[^\w\'\"\@\-\.\]/i', '',($_POST["username"])))){
		echo "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><br />Please go back, USERNAME is only allowed to have letters and numbers with NO SPACES MUST BE BETWEEN 5 to 10 characters.<p>&nbsp;</p><p>&nbsp;</p><a href='javascript:javascript:history.go(-1)'>Click here to go back to previous page</a>";

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
could be simpler to check the first character of the username

then check if the rest are letter or number.

looking at your regular expression it looks like you are checking what to exclude. You should be checking for includes.
I believe the regex would be something like:

/[a-Z][a-Z, 0-9]*/


I'm getting this as an error: Warning: eregi() [function.eregi]: REG_ERANGE
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
I worked on this for a while using 'preg_match' because 'eregi' is being deprecated.  I finally got it working below.  Regular Expressions still drive me nuts...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<title>PHP Preg-match</title>
<h1>PHP Preg-match</h1>
<form action="" method="post">
<input type="text" name="username" />
<input type="submit" name="submit" value="Submit" />

if(isset($_POST["submit"])) {
$usrnm = $_POST["username"];
$usrnm = trim($usrnm);
$pttrn = "/[A-Z,a-z][A-Z,a-z,0-9]*/";
echo "Username: $usrnm - ";
echo $pttrn."<br>";;
preg_match($pttrn, $usrnm, $matches);

if($usrnm != $matches[0]) echo "Not a valid username.";
else echo "Good username.";


Open in new window


I'm sorry to split the points but I'm still getting errors. I'm going to post the entire script and maybe take a look at the problem. Thanks!

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