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