Hi i am trouble adding NO CAPS and no spaces in the username on the code below

I want the username to be character from 3 to 30
I want to restrict members from adding spaces or caps

      //-----------------------------------------
      //  Check valid user name
      //-----------------------------------------
      if ( !$username OR strlen($username) < 3  OR strlen($username) > 30 )
      {
          $errors[] = $lang['error_user_invalid'];
      }
Member Avatar for skinbug

a regex would be the way to go, such as...

if(!$username || strlen($username = trim($username)) == 0) {
$errors[] = ...Please enter a username...;
}
else {
$username = stripslashes($username) // sql inject clean
$regex = "/^[a-z]+$/";
if(!preg_match($regex, $username)) {
$errors[] = ...username invalid...;
}
else if(strlen($username) < 3) {
$errors[] = ...username too short...;
}
else if(strlen($username) > 30) {
$errors[] = ...username too long...;
}
}

// END

That regex has worked for me...you can always look into them more as they can go into a lot more detail, unless someone else has another solution.

edit...there needs to bea semi colon after the stripslashes line

I got Parse error: syntax error, unexpected T_VARIABLE

on $regex = "/^[a-z]+$/";

whats wrong with it

Member Avatar for skinbug

what line do you get the syntax error on? It should tell you the line number.

In my first post, I put an edit that says I missed a semi-colon after the stripslashes line - check it out, as if you do not add this semi-colon, you will get a syntax error.

Add it where there is the comment about sql inject clean and see if you still get a syntax error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.