ctype_alnum(): To check alphanumeric string

$var="10af4g4"; // This is  alphanumeric
//$var="abc=123"; // This is not alphanumeric
//$var="123"; // This is  alphanumeric
//$var='xyz'; // This is  alphanumeric
//$var='123.56'; // This is not alphanumeric
//$var=123;  // This is not alphanumeric

	echo "<b>$var</b> This is  alphanumeric ";
	echo " <b>$var</b> This is not alphanumeric";
This is  alphanumeric
bool ctype_alnum ( string $input_string )
$input_string : String to be checked.

ctype_alnum is a PHP filter function checks presence of all alphanumeric characters. It allows a to z characters and 0 to 9 numbers. Any thing other than this if present then it return FALSE.

You can change the value of the variable $var in above script and see the result. Decimal points are not allowed ( False ) in ctype_alnum.

We can check the form data input by users directly by using ctype_alnum function.

Here is one example.
echo " This is alphanumeric ";
}else{ echo "this is not alphanumeric";}

Checking array elements

foreach ($ar as $var){
	echo " <b>$var</b>  is alphanumeric  ";
	echo " <b>$var</b> is NOT alphanumeric ";
echo "<BR>";
abc  is alphanumeric
as12  is alphanumeric  
123 is NOT alphanumeric 
ab#1 is NOT alphanumeric 
@12 is NOT alphanumeric 

Removing blank space before using

It is better to use string function trim to remove blank space present at starting and ending of the variable before using it. Users some time add blank space and this goes wrongly as not alphanumeric variable. Here is the modified script to remove blank space.
$var=trim($var); // remove space at starting and ending of variable. 
echo " This is alphanumeric ";
}else{ echo "this is not alphanumeric";}
echo "<br><br>";// 
echo " Inside if <br>";

Allowing blank space and &

Using str_replace() we can remove space and '&' ( without quotes ) and check. Here is the code.
$search  = array('&', ' ');
$replace = array('', '');

echo "Data Error ";
For easy understanding here is the form to submit data to above script
<form method=post action='ctype_alnck.php'>
<input type=text name=var>
<input type=submit value=submit>
Here if you want to check only numeric numbers then better to use is_numeric() function.
STRING REFERENCE Check for only alphabetic characters by ctype_alpha() Check for all lower or upper Check for at least one lower case or upper case

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net




    Well I am using this very same thing but when I submit the form I get the error although I am submitting alphanumerical usernames, I have tried alpha, alphanumeric, and numeric I get the same error.

    if(!isset($username) or strlen($username) <3){
    $msg=$msg."User id should be =3 or more than 3 char length<BR>";
    $status= "NOTOK";}

    $msg=$msg."User id should contain alphanumeric chars only<BR>";
    $status= "NOTOK";}

    Any ideas why it keeps throwing at me this error ?
    Any suggestions ?
    Thanks a lot


    You may have to remove blank space from the variable. Sample code is added now.


    Yes thanks
    I have tried that, earlier but I had to rebuild the whole script with the exact same code, and now it is solved, no apparent reason whatsoever.

    Thanks again


    good day, the function ctype_alnum for alphanumeric can't work for me.. just like when i change this code from

    echo "Data Error";



    echo " This is alphanumeric ";
    }else{ echo "this is not alphanumeric";}

    and i will change the value of cat_id int into a alphanumeric.. i cant see the value of a subcategory ..

    help me please .. thanks!


    i get the same error sir, but cant fix it ..
    here is my code .. help me please! Thanks in advance

    echo " This is alphanumeric ";
    }else{ echo "this is not alphanumeric";}
    echo "<br><br>";
    echo " Inside if <br>";

    $quer2="SELECT DISTINCT ClientID,ClientID FROM creport order by ClientID";

    if(isset($cat) and strlen($cat) > 0){
    $quer="SELECT DISTINCT UnitId FROM ureport where ClientID=$cat order by ClientID";
    }else{$quer="SELECT DISTINCT UnitId FROM ureport order by UnitId"; }


    There is no reason why it should not work, check the value of $cat. Print the output and see are you getting the correct value or not.

    Post your comments , suggestion , error , requirements etc here

    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer