9three Posted July 23, 2009 Share Posted July 23, 2009 Hey I have this pattern: ^([a-zA-Z])|(0-9) ([a-zA-Z])|(0-9)$ What I'm trying to accomplish: 1. Start with AND end with a alphabetic or number 2. Allow only alphabetic and numbers in the entire string. Did I do it right? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/167087-solved-allowing-alphabetic-and-numbers-only/ Share on other sites More sharing options...
Daniel0 Posted July 23, 2009 Share Posted July 23, 2009 If it starts with and ends with either an alphanumeric character, and the string is only allowed to be comprised of alphanumeric characters, isn't that just ^[a-zA-Z0-9]+$ (or ctype_alnum)? Or am I missing something? Quote Link to comment https://forums.phpfreaks.com/topic/167087-solved-allowing-alphabetic-and-numbers-only/#findComment-881004 Share on other sites More sharing options...
9three Posted July 23, 2009 Author Share Posted July 23, 2009 ctype_alnum() would work. But isn't that function for either or? Not both? At least that's what I understand from the documentation. Quote Link to comment https://forums.phpfreaks.com/topic/167087-solved-allowing-alphabetic-and-numbers-only/#findComment-881005 Share on other sites More sharing options...
Daniel0 Posted July 23, 2009 Share Posted July 23, 2009 ctype_alnum() checks if a string solely consists of alphanumeric characters. That is the alphabetic characters a-z and A-Z and the numeric characters 0-9. It's essentially the same as the regex I posted. Quote Link to comment https://forums.phpfreaks.com/topic/167087-solved-allowing-alphabetic-and-numbers-only/#findComment-881008 Share on other sites More sharing options...
nrg_alpha Posted July 23, 2009 Share Posted July 23, 2009 Theone caveat involving the use of functions like ctype_alnum() is your locale. I explain it in this thread (reply #3). Quote Link to comment https://forums.phpfreaks.com/topic/167087-solved-allowing-alphabetic-and-numbers-only/#findComment-881178 Share on other sites More sharing options...
9three Posted July 23, 2009 Author Share Posted July 23, 2009 Great. thank you. Quote Link to comment https://forums.phpfreaks.com/topic/167087-solved-allowing-alphabetic-and-numbers-only/#findComment-881197 Share on other sites More sharing options...
9three Posted July 24, 2009 Author Share Posted July 24, 2009 When I use the pattern you provided it gives me an error if (!preg_match('^[a-zA-Z0-9]+$', $strUsername)) ^[a-zA-Z0-9]+$ Warning: preg_match() [function.preg-match]: No ending delimiter '^' found I tried somethings like adding a / at the end but it was no good. Quote Link to comment https://forums.phpfreaks.com/topic/167087-solved-allowing-alphabetic-and-numbers-only/#findComment-882144 Share on other sites More sharing options...
nrg_alpha Posted July 24, 2009 Share Posted July 24, 2009 This is because in preg, you need to encase the entire pattern in delimiters... so in your case, you can use: if (!preg_match('#^[a-z0-9]+$#i', $strUsername)) // note the opening and closing #.. I also used the i modifier for case insensitivity Delimiters can be any non alphanumeric, non whitespace ASCII character (except a backslash). You can read up about delimiters here: http://www.phpfreaks.com/forums/index.php/topic,95777.0.html As well as the pcre portion of the manual. Quote Link to comment https://forums.phpfreaks.com/topic/167087-solved-allowing-alphabetic-and-numbers-only/#findComment-882149 Share on other sites More sharing options...
9three Posted July 24, 2009 Author Share Posted July 24, 2009 thanks friend! Quote Link to comment https://forums.phpfreaks.com/topic/167087-solved-allowing-alphabetic-and-numbers-only/#findComment-882197 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.