redarrow Posted April 7, 2009 Share Posted April 7, 2009 advance thank you. how the hell it letting the @ in, when only a-z 0-9 are allowed only. madness <?php $message="my name is @ redarrow"; $err="Sorry you have not filled in the form correctly!"; if(!preg_match("/[a-z0-9]/i",$message)){ echo $err; }elseif(empty($message)){ echo $err; }else{ echo $message; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152993-regular-expression-problem/ Share on other sites More sharing options...
AmandaF Posted April 7, 2009 Share Posted April 7, 2009 It's only matching one character. /[a-z0-9]/i means "match anything that contains a letter or number". What you want is /^[a-z0-9]*$/i or /^[a-z0-9]+$/i which will match either zero or more (if you use the *) or one or more (with the +) letters and numbers in between the beginning (^) and end ($) of the string (so nothing but letters and numbers will be accepted). Quote Link to comment https://forums.phpfreaks.com/topic/152993-regular-expression-problem/#findComment-803574 Share on other sites More sharing options...
Axeia Posted April 7, 2009 Share Posted April 7, 2009 What she said, and I wanted to add: There is a regular expressions subforum (in this one even). Might yield better results there in the future. Quote Link to comment https://forums.phpfreaks.com/topic/152993-regular-expression-problem/#findComment-803580 Share on other sites More sharing options...
nrg_alpha Posted April 7, 2009 Share Posted April 7, 2009 It's only matching one character. /[a-z0-9]/i means "match anything that contains a letter or number". What you want is /^[a-z0-9]*$/i or /^[a-z0-9]+$/i which will match either zero or more (if you use the *) or one or more (with the +) letters and numbers in between the beginning (^) and end ($) of the string (so nothing but letters and numbers will be accepted). There is a missing element to the character list however.. Since the goal is the check a message, you need to allow spaces (otherwiseallyourmessagewordswillneedtobebunchedtogether). So, assuming there is no punctuation (which in itself is weird), and the message can contain zero or more characters (which I assume is also the case if there is a empty($message) check), simply add the space to the zero or more character list: /^[ a-z0-9]*$/i If we want to include various whitespace characters, we can use \s instead of a literal space. But all in all, I question the entire message system and the pattern that implies, simply for the sake that messages in real life contain more than [ a-z0-9]. As mentioned previously, this means no punctuation (so kiss periods, question marks, commas and the like goodbye). Quote Link to comment https://forums.phpfreaks.com/topic/152993-regular-expression-problem/#findComment-803682 Share on other sites More sharing options...
ghostdog74 Posted April 8, 2009 Share Posted April 8, 2009 advance thank you. how the hell it letting the @ in, when only a-z 0-9 are allowed only. madness <?php $message="my name is @ redarrow"; $err="Sorry you have not filled in the form correctly!"; if(!preg_match("/[a-z0-9]/i",$message)){ echo $err; }elseif(empty($message)){ echo $err; }else{ echo $message; } ?> $message="my name is @ redarrow"; if ( ctype_alnum ($message) ){ print "$message is alphanumeric\n"; }else{ print "'$message' is not alphanumeric\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/152993-regular-expression-problem/#findComment-804170 Share on other sites More sharing options...
nrg_alpha Posted April 8, 2009 Share Posted April 8, 2009 $message="my name is @ redarrow"; if ( ctype_alnum ($message) ){ print "$message is alphanumeric\n"; }else{ print "'$message' is not alphanumeric\n"; } ctype_alnum doesn't evaluate to true when there are spaces involved, so this would mean messages cannot have spaces. Quote Link to comment https://forums.phpfreaks.com/topic/152993-regular-expression-problem/#findComment-804188 Share on other sites More sharing options...
ghostdog74 Posted April 8, 2009 Share Posted April 8, 2009 ctype_alnum doesn't evaluate to true when there are spaces involved, so this would mean messages cannot have spaces. i only looked at his first post. if its not what he wants, then so be it. Quote Link to comment https://forums.phpfreaks.com/topic/152993-regular-expression-problem/#findComment-804310 Share on other sites More sharing options...
nrg_alpha Posted April 8, 2009 Share Posted April 8, 2009 Yeah, I know he mentions only a-z0-9, and if that's the case, then for sure, ctype_alnum would be the preferred method. I just found it strange that for a message, only those characters are permissible. Quote Link to comment https://forums.phpfreaks.com/topic/152993-regular-expression-problem/#findComment-804693 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Yeah, I know he mentions only a-z0-9, and if that's the case, then for sure, ctype_alnum would be the preferred method. I just found it strange that for a message, only those characters are permissible. Same here. If you use ctype_alnum, it would be more difficult to modify than using a regex from the start. But, yes, in this case ctype_alnum, would be ideal. Quote Link to comment https://forums.phpfreaks.com/topic/152993-regular-expression-problem/#findComment-804847 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.