AlphaNum validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

AlphaNum validation

Post 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;
}
?>
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Code: Select all

function is_alphanumeric($test) {
return (preg_match("/^[a-z0-9 ]+$/i", $test));
}
*Not My Code Though*
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

I don't have much of a clue of your question but str_split is a PHP5 function by the way :).

-Nay
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

hmmm...

Code: Select all

function CheckAlfaNum($strdata){
  return ereg("^[[]]+$",$strdata);
}
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

Thanks a lot :).

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