Jay wrote:
I got this code from here, i think it was "Ni Shurong" who gave it to me. The code works pretty good. Except i found that sending "asdas." into the function will return true...it should return false. What i am trying to do is check for valid chars. Only alpha and numeric. Can someone help me correct the code:
function isValid($strData, $bIncludeAlpha=false, $bIncludeNumber=false) { switch(true) { case $bIncludeAlpha && !$bIncludeNumber: $ptr = "/^[a-zA-Z]+/";
Match one or more letters at the beginning of the string: "asdas." passes.
break;
case !$bIncludeAlpha && $bIncludeNumber:
$ptr = "/^[0-9]+/";
match one or more letters at the beginning of the string: "asdas." fails
break;
case $bIncludeAlpha && $bIncludeNumber:
$ptr = "/^[a-zA-Z0-9]+/";
Match one or more letters or numbers at the beginning of the string: "asdas." passes
break;
default:
return false;
}
return preg_match($ptr, $strData);
}
Your patterns are missing the $ for "end of string", if you're after what I think you are.
/^[a-zA-Z]+$/ => ensures entire string is letters only (and not empty)
/^[0-9]+$/ => ensure entire string is numbers only (and not empty)
/^[a-zA-Z0-9]+$/ => ensures entire string is letters or numbers only (and not empty)
--
---John Holmes...
Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/
php|architect: The Magazine for PHP Professionals – www.phparch.com
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

