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

if(ctype_alnum($var)){
	echo "<b>$var</b> This is  alphanumeric ";
}else{ 
	echo " <b>$var</b> This is not alphanumeric";
}
output
This is  alphanumeric
Syntax
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.
if(ctype_alnum($_POST['var'])){
echo " This is alphanumeric ";
}else{ echo "this is not alphanumeric";}

Checking array elements

$ar=array('abc','as12',123,'ab#1','@12');
foreach ($ar as $var){
if(ctype_alnum($var)){
	echo " <b>$var</b>  is alphanumeric  ";
}else{ 
	echo " <b>$var</b> is NOT alphanumeric ";
}
echo "<BR>";
}
output
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.
<?Php
$var=$_POST['var'];
$var=trim($var); // remove space at starting and ending of variable. 
if(ctype_alnum($var)){
echo " This is alphanumeric ";
}else{ echo "this is not alphanumeric";}
echo "<br><br>";// 
if(!ctype_alnum($var)){
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('', '');

if(!ctype_alpha(str_replace($search,$replace,$string_var))){
echo "Data Error ";
exit;
}
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>
</form>
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
ctype()

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Adan

    06-12-2014

    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";}

    if(!ctype_alnum($username)){
    $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
    smo

    06-12-2014

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

    07-12-2014

    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
    rie

    09-12-2014

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

    if(!is_numeric($cat_id)){
    echo "Data Error";
    exit;
    }

    to

    $var=$_GET['cat'];

    if(ctype_alnum($var)){
    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!
    patrick

    11-12-2014

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

    <?Php
    $cat=$_GET['cat'];
    $cat=trim($cat);
    if(ctype_alnum($cat)){
    echo " This is alphanumeric ";
    }else{ echo "this is not alphanumeric";}
    echo "<br><br>";
    if(!ctype_alnum($cat)){
    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"; }
    smo

    12-12-2014

    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