Page 1 of 1

string validation... can't figure out the error

Posted: Wed Dec 19, 2007 6:31 am
by pedrotuga
The code explains itself...
It's a 8 alphanumeric characters string validation.

Why doesnt it work?

Code: Select all

function _check_valid_hash($hash){
	
		$hash = strtolower($hash);
	
		if (preg_match("#[a-z0-9]*8#",$hash)){
			return true;
		}else{
			return false;
		}
	}

EDIT:
Ok, i checked some regex docs more carefuly

Code: Select all

preg_match("|^[a-z0-9]{8}$|",$hash);
sorry.

Posted: Wed Dec 19, 2007 7:33 am
by Zoxive

Code: Select all

$hash = strtolower($hash);
Not needed.

Code: Select all

[a-zA-Z0-9]{8}
{8} Means Exactly 8 characters.

{8,} Means At Least 8 characters.

Posted: Wed Dec 19, 2007 2:59 pm
by John Cartwright
Zoxive wrote:

Code: Select all

$hash = strtolower($hash);
Not needed.

Code: Select all

[a-zA-Z0-9]{8}
{8} Means Exactly 8 characters.

{8,} Means At Least 8 characters.
I prefer the i (insensitivity) modifier :)

Posted: Tue Jan 08, 2008 1:07 am
by vapoorize
why use regex at all when php has built in function for checking types? :)
http://us3.php.net/manual/en/function.ctype-alnum.php

check length
http://www.php.net/strlen

Posted: Tue Jan 08, 2008 10:50 am
by pickle
vapoorize wrote:why use regex at all when php has built in function for checking types? :)
http://us3.php.net/manual/en/function.ctype-alnum.php
Wow - nice find!