Page 1 of 1

AlphaNum validation

Posted: Thu Jan 08, 2004 6:09 am
by fastfingertips
I made this function to validate a alpha numeric but i'm a beginner in PHP so i would like to know if is there any other way.

Code: Select all

<?php
function CheckAlfaNum($strdata)
{
    $charset = "abcdefghijklmnopqrstuvwxyz0123456789"; 
    $length=strlen($strdata);
    $length=$length-1;    
    $check=str_split($strdata);
    for ($i=0;$i<=$length;$i++)
    {
        $itisthere = stripos($charset, $check[$i]);
        if($itisthere === false)
        {
            return 1;
        }
    }
    return 0;
}
?>

Posted: Thu Jan 08, 2004 6:16 am
by Pyrite

Code: Select all

function is_alphanumeric($test) {
return (preg_match("/^[a-z0-9 ]+$/i", $test));
}
*Not My Code Though*

Posted: Thu Jan 08, 2004 6:18 am
by Nay
I don't have much of a clue of your question but str_split is a PHP5 function by the way :).

-Nay

Posted: Thu Jan 08, 2004 6:22 am
by Weirdan
hmmm...

Code: Select all

function CheckAlfaNum($strdata){
  return ereg("^[[]]+$",$strdata);
}

Posted: Thu Jan 08, 2004 6:22 am
by Pyrite
Or explained better yet ..

http://us3.php.net/manual/en/function.ctype-alnum.php


Editors note:

CTYPE functions are always prefered, when possible, to Regular Expressions and, often, even to some equivalent str_*, is_* functions. This is because of the fact that CTYPE uses a native C library and thus processes signitificaly faster.

Maxim Maletsky

Posted: Thu Jan 08, 2004 6:27 am
by fastfingertips
Thanks a lot :).

I knew it, anyway i have reinvented the wheel, isn’t it? :D :oops: