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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

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

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 :)
vapoorize
Forum Newbie
Posts: 22
Joined: Mon Dec 17, 2007 5:35 pm

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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!
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply