I am not quite understanding how this how ereg and Preg_match thing works. I am trying to prevent users from creating a username that is less than 3 characters, with no capital letters, and no special characters and cannot be a reserved user id such as root, admin, and operator and can not include things like dashes, spaces or !@#$%(special characters)

I am wondering if I am headed in the right direction

this is what I have so far

$pattern = '/[^A-Z0-9[:space:].|-|,|$|!|?|%|!]{3,25}$/';

if ($password==$repeatpassword)
     {
       if (preg_match($pattern, $username)) {


       echo "Username must be be at least 3 characters, all lowercase and contain no special characters";

Recommended Answers

All 4 Replies

First don't use ereg as it has been deprecated. The syntax is correct for the preg_match but the logic may not be doing exactly what you want. Also I don't think your regex you have will work for what you are wanting to do.

I can invest a little more time in a bit but I am stuck on a conference call at the moment :(

Thanx I am just now noticing that I put ereg...I am trying preg_match in my code...I am just trying to make sure that I can make sure the username does not contain the special characters I mentioned above..I think what I have so far will take care of the lower case and the (at least 3 character) requirement...whenever you can get back to me would be great...thanks for your reply

Well I tested your code and it wasn't working until I altered the regex as you can see below. Once I did that I added some more code to check if the string is 3 or more characters long. I like doing it that way as well so I can provide different feed back and sort of stagger my error checking.

<?php

$username = 'username';

$pattern = '/[A-Z0-9[:space:].|-|,|$|!|?|%|!]/';

if (preg_match($pattern, $username)) {

	echo "Your username must be all lowercase and cannot have any numbers or special characters.";

} else {

	if (strlen($username) < 3) { // check to make sure that the string is greater than 2 characters
		
		echo "Your username must be 3 characters or more.";
		
	} else {

		echo "The username is GOOD";

	}
}
?>

thank you so much for your help

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.