cooldude832 Posted January 7, 2008 Share Posted January 7, 2008 I am trying to make sure passwords are only alphanumeric so I have <?php $pattern = "[^A-Za-z0-9]"; if(eregi($values['password'],$pattern)){ $errors[] = "The <b>password</b> can only contain alphanumeric characters."; } ?> so that should return an error if it finds a non Alphanumeric string, but it fails any ideas? Link to comment https://forums.phpfreaks.com/topic/84813-trying-to-match-only-alphanumeric-strings/ Share on other sites More sharing options...
rajivgonsalves Posted January 7, 2008 Share Posted January 7, 2008 I don't know about ereg but preg would be $pattern = "/[^A-Za-z0-9]+/"; if(preg_match($pattern,$values['password'])){ $errors[] = "The <b>password</b> can only contain alphanumeric characters."; } Link to comment https://forums.phpfreaks.com/topic/84813-trying-to-match-only-alphanumeric-strings/#findComment-432372 Share on other sites More sharing options...
dsaba Posted January 7, 2008 Share Posted January 7, 2008 <?php $pat = '[a-zA-Z0-9]+'; $str = 'hello23489jhksgjl'; if (ereg($pat, $str)) { echo 'it is alphanumeric!'; } else { echo 'no its not alphanumeric!'; } ?> Tested: http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=2k3octs2 Link to comment https://forums.phpfreaks.com/topic/84813-trying-to-match-only-alphanumeric-strings/#findComment-432863 Share on other sites More sharing options...
teng84 Posted January 8, 2008 Share Posted January 8, 2008 why do people keep on looking regex when there is predefine function that might match letters numbers and email people keep on asking how to match letters numbers etch.. and they get regex solution OK LOOK at ctype data they might what you're looking for I don't know about ereg but preg would be $pattern = "/[^A-Za-z0-9]+/"; if(preg_match($pattern,$values['password'])){ $errors[] = "The <b>password</b> can only contain alphanumeric characters."; } <?php $pat = '[a-zA-Z0-9]+'; $str = 'hello23489jhksgjl'; if (ereg($pat, $str)) { echo 'it is alphanumeric!'; } else { echo 'no its not alphanumeric!'; } ?> Tested: http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=2k3octs2 Link to comment https://forums.phpfreaks.com/topic/84813-trying-to-match-only-alphanumeric-strings/#findComment-433217 Share on other sites More sharing options...
dsaba Posted January 8, 2008 Share Posted January 8, 2008 that's a good suggestion teng, but why we answered in regex solutions was because this was his question Link to comment https://forums.phpfreaks.com/topic/84813-trying-to-match-only-alphanumeric-strings/#findComment-433231 Share on other sites More sharing options...
teng84 Posted January 8, 2008 Share Posted January 8, 2008 that's a good suggestion teng, but why we answered in regex solutions was because this was his question yes but i guess better to point to better solution. i always see threads asking for regex and get an regex solution but in fact that is doable using sting function etc.. @dsaba i dont mean anything about this post hope you see my point thanks Ronald(teng) Link to comment https://forums.phpfreaks.com/topic/84813-trying-to-match-only-alphanumeric-strings/#findComment-433251 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.