pianoman993 Posted June 30, 2008 Share Posted June 30, 2008 How do I validate a $username string by checking if it contains charactors such as ?!-+= etc... ? I'm not sure if your supposed to use eregi or preg_match or what. If someone knows the answer please post your code below. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/112641-solved-simple-eregi-question/ Share on other sites More sharing options...
effigy Posted June 30, 2008 Share Posted June 30, 2008 What are the valid characters for a user name? Quote Link to comment https://forums.phpfreaks.com/topic/112641-solved-simple-eregi-question/#findComment-578458 Share on other sites More sharing options...
pianoman993 Posted June 30, 2008 Author Share Posted June 30, 2008 Alphanumeric values and hyphens. There can be no hyphen at the beginning of the string. Quote Link to comment https://forums.phpfreaks.com/topic/112641-solved-simple-eregi-question/#findComment-578461 Share on other sites More sharing options...
effigy Posted June 30, 2008 Share Posted June 30, 2008 What about min and max lengths? No underscores? <?php if (!preg_match('/\A(?!-)[-a-z0-9]+\z/', $string)) { echo 'Invalid'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/112641-solved-simple-eregi-question/#findComment-578492 Share on other sites More sharing options...
pianoman993 Posted June 30, 2008 Author Share Posted June 30, 2008 As for length I have a if(!substr($username,6,20)) {} condition. Underscores are not permitted. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/112641-solved-simple-eregi-question/#findComment-578521 Share on other sites More sharing options...
effigy Posted June 30, 2008 Share Posted June 30, 2008 substr does not enforce limits. You can consolidate this by exchanging the + with {6,20}. Quote Link to comment https://forums.phpfreaks.com/topic/112641-solved-simple-eregi-question/#findComment-578526 Share on other sites More sharing options...
pianoman993 Posted June 30, 2008 Author Share Posted June 30, 2008 Great, i will do that thanks. Oh and one more thing, how do I make it so that a hyphen can not be the last character of the $username string? Quote Link to comment https://forums.phpfreaks.com/topic/112641-solved-simple-eregi-question/#findComment-578532 Share on other sites More sharing options...
effigy Posted June 30, 2008 Share Posted June 30, 2008 /\A(?!-)[-a-z0-9]{6,20}(?<!-)\z/ Quote Link to comment https://forums.phpfreaks.com/topic/112641-solved-simple-eregi-question/#findComment-578543 Share on other sites More sharing options...
pianoman993 Posted July 2, 2008 Author Share Posted July 2, 2008 Quick question, I've been trying to figure out a way to use this preg_match function if (!preg_match('/\A(?!-|_)[-_a-zA-Z0-9]{6,20}|(?<!-|_)\z/', $username)) but add an additional rule allowing only ONE space in the $username string. I fear the solution is aggravatingly simple Quote Link to comment https://forums.phpfreaks.com/topic/112641-solved-simple-eregi-question/#findComment-579798 Share on other sites More sharing options...
effigy Posted July 2, 2008 Share Posted July 2, 2008 if (substr_count($username, ' ') >= 2 || !preg_match('/\A(?![-_ ])[-\w ]{6,20}(?<![-_ ])\z/', $username) ) { ... } Quote Link to comment https://forums.phpfreaks.com/topic/112641-solved-simple-eregi-question/#findComment-580030 Share on other sites More sharing options...
pianoman993 Posted July 2, 2008 Author Share Posted July 2, 2008 Thanks man Quote Link to comment https://forums.phpfreaks.com/topic/112641-solved-simple-eregi-question/#findComment-580458 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.