Ctype_alnum to check alphanumeric characters

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.

This is a good function to check data coming from insecure sources like user input data. Before storing or processing such data we can use this function check to match our requirement. Here is the syntax
bool ctype_alnum($text)
Now let us try with some examples.
$var="10af4g4";

if(ctype_alnum($var)){
echo " This is  alphanumeric ";
}else{ echo "this is not alphanumeric";}
You can change the value of the variable $var 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";}

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

Visitors Rating
Your Rating




Google+
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 Comment This is for short comments only. Use the forum for more discussions.




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer