PHP invalid characters

Spacemail by SpaceshipSpacemail by Spaceship
Watch

SiKing

Registered MemberEstablished Member
Impact
6
Just a quicko, how can I define invalid characters for a user's input so that when a user registers to my site, they don't select a username such as:

"#~@L@N J0N3S~#"

lol, excuse the example. I only really want letters, numbers and hyphens to be used. Thanks :)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains — AI StorefrontUnstoppable Domains — AI Storefront
http://us2.php.net/ctype_alpha

I use these functions, but i have a class that i use them, but return false if its empty. I will think about posting it or not ;)

Here it is... heh

PHP:
function is_alpha($str = '') //alpha
	{
		if($str == "")
		{
			return false; //string empty
		}
		elseif(ctype_alpha($str) == true)
		{
			return true;
		}
		else
		{
			return false; //error, there must be a character than is not a letter!
		}
	}
	function is_num($str = '') //digits
	{
		if($str == "")
		{
			return false; //string empty!
		}
		elseif(ctype_digit($str) == true)
		{
			return true; //its a non-decimal number! (base 10)
		}
		else
		{
			return false; //error, there must be a character than is not a digit!
		}
	}
	function is_alnum($str = '') //alphanumeric
	{
		if($str == "")
		{
			return false; //string empty
		}
		elseif(ctype_alnum($str) == true)
		{
			return true; //this is 0-9, a-z!
		}
		else
		{
			return false; //error, there must be a character that is not a digit or letter!
		}
	}

Well some of it... i commented it pretty well, if you have any problems just ask.
 
Last edited:
0
•••
Thanks very much for the help :)
 
0
•••
You should also be able to do a POSIX match.

Something like this should work too:

PHP:
$string = $_POST[string_to_test];
if(ereg('[^@.[:alnum:]]',$string)) {
  # code that catches invalid charactes here
}

In this example, the character class is negated with the beginning '^', and you would put any *VALID* characters in the class. In this case, you would allow the "@", a ".", and any alphanumbers (A-Z, a-z, 0-9)

-Bob
 
0
•••
Thanks Bob, I am currently using

PHP:
if (!ctype_alnum($username)) {
       
      echo '<script>alert("The username must consist of only alphanumeric characters (a-z 0-9).");</script>';
echo '<script>history.back(1);</script>';
exit;
   }

Is this wise?
 
0
•••
I do like Bob suggested. A regular expression is fine and more flexible IMO.

For example I use code like this to test a user name, that means only letters and figures are allowed and the string must be between 4 and 16 chars long. It does the job nicely :]
PHP:
if (!eregi("^([a-zA-Z0-9]{4,16})$", $username )) {
echo "Error: the user name is not valid blah...";
 
0
•••
sdsinc, I just changed my code to the code you posted. But how do I make it case sensitive?
 
0
•••
Well you just change eregi to ereg
 
0
•••
Ah, simple as that eh? Thanks a lot
 
0
•••
I use eregi a lot when validating email addresses and other forms of data - very useful.

If you want to see other ways it can be used, check out the PHP Manual documentation
http://us2.php.net/eregi

-Steve
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
CatchedCatched

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back