jacko310592 Posted March 25, 2010 Share Posted March 25, 2010 hey guys, i have to following function which i need to cross-check data with to make sure the data only contains A-Z a-z 0-9 and _ but at the moment, it seems to let through all symbols function alphanumericDatacheck($data){ return preg_match ("^[a-zA-Z0-9_]$^", $data); } //--- Useage example... if (alphanumericDatacheck($username) == false){ $usernameErr = "Can only contain Letters (A-Z), Numbers (0-9) and Underscore (_)"; } can anyone suggest where im going wrong, im really no good with expressions thanks Link to comment https://forums.phpfreaks.com/topic/196503-preg_match-expression/ Share on other sites More sharing options...
jacko310592 Posted March 25, 2010 Author Share Posted March 25, 2010 i found the following expression which seems to solve my problem: #^[a-z0-9_]+$#i this allows A-Z a-z 0-9 and _ but can anyone explain this to me.. as 'A-Z' isint included in the expression, how does this allow caps? and what does the "#^" and "+$#i" parts mean? thanks Link to comment https://forums.phpfreaks.com/topic/196503-preg_match-expression/#findComment-1031699 Share on other sites More sharing options...
cags Posted March 25, 2010 Share Posted March 25, 2010 The hashes are delimiters these as the name suggest delimit a pattern, it doesn't have to be the hash character. It also separates the pattern from the modifiers. The i at the end is a modifier and it means make the pattern case insensitive, which is why it picks up capital letters also. The ^ character matches the start of the string and the $ matches the end of the string. The + is a quantifier it means match 1 or more characters from the character class. Link to comment https://forums.phpfreaks.com/topic/196503-preg_match-expression/#findComment-1031752 Share on other sites More sharing options...
jacko310592 Posted March 25, 2010 Author Share Posted March 25, 2010 thanks cags, that cleared everything up nicely (: Link to comment https://forums.phpfreaks.com/topic/196503-preg_match-expression/#findComment-1031759 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.