Jump to content

Recommended Posts

Hello,

 

I am just looking to see if this is the best way to check a valid username. The requirements I want are in the comment. Any suggestions would be great, thanks!

 

<?php
$userArray = array("username", "test1234testsetset", "test-test-3est455test", "#dfdf45-sfdsf", "'sdfsdf+__", "good_username", 
				"bad_username%", "bad123", "good12", "goodgood1234", "goodbad1234", "bad1234", "bad123ba4", "b1c2v3b4n5m6", "toolongofausernameok", "sh", "sht", "bad cause spaces", "bad12345b", "good_but-bad", "gooduser", "frost", "david", "creative210", "12345321");

foreach ($userArray as $user) {
if (!validUsername($user)) {
	echo "BAD: " . $user . "<Br />";
}else {
	echo "GOOD: " . $user . "<br />";
}
}					

/**
* Checks if a username is valid
*
* Takes in a string of username. If the username is valid
* which means that there are no special characters, it has
* at least 1 letter, the start of it does not start with a number
* and the length is between 4 and 15. And it does not have more than
* 4 numbers in it. For every number there must be at least 2 
* letters.
**/
function validUsername($user) {
// first check the length. No need to continue if the length, or first is wrong.
$firstNum = substr($user, 0, 1);
if ((strlen($user) < 4 || strlen($user) > 15) || is_numeric($firstNum)) 
	return false;

$checkNum = preg_replace('/([a-z])*/i', "", $user);
if (strlen($checkNum) > 4 || (strlen($checkNum) > 0 && (strlen($user) - strlen($checkNum)) < (strlen($checkNum) * 2)))
	return false;

preg_match('/(^[a-z0-9]*)/i', $user, $matches);
if ($matches[1] != $user) 
	return false;

return true;
}
?>

 

I think it is pretty efficient as is, but yea since I am new the regex part just wanted to check.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/135838-code-critique-validusername/
Share on other sites

Heres an example with error messages to make it more use friendsly

 

<?php
$Username = "test"; 
//Must have atleast 1 number

$Username = "1test"; 
//Must Start with a letter

$Username = "test1"; 
//Username: test1 is Okay

$Username = "tes"; 
//Must Start with a letter 
//Must have atleast 1 number
//Must have twice as many letters to numbers

$Username = "te12";
//Must have twice as many letters to numbers

$Username = "te12fghdsgfhsdgfsd gfhdsgfhdsgfhsdgfhgdsfjgsdfhsdfgsdhj";
//Must have twice as many letters to numbers

$Username = "test 12";
//Must NOT contain special chars
//Must have twice as many letters to numbers


$error = array();

$letters = strlen(preg_replace('/\d/i', '', $Username));
$numbers = strlen(preg_replace('/[a-z]/i', '', $Username));
$total = $numbers+$letters;
if ($Username != preg_replace('/[^\da-z]/i', '', $Username))
{
$error[] = "Must NOT contain special chars";
}

if (!preg_match('/^[a-z]\w{3}/i', $Username))
{
$error[] = "Must Start with a letter";
}
if($numbers > 4)
{
$error[] = "Must have less than 4 numbers";
}
if( $numbers < 1)
{
$error[] = "Must have atleast 1 number";
}
if( ($numbers*2) > $letters)
{
$error[] = "Must have twice as many letters to numbers";
}

if( $total > 15 || $total < 4) 
{
$error[] = "Must have twice as many letters to numbers";
}
if(count($error)>0)
{
echo implode($error, "<br>");
}else{
echo "Username: $Username is Okay";
}

?>

 

Hope this helps

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.