bearruler Posted July 21, 2006 Share Posted July 21, 2006 I have a little experience with Regular expressions, but this one is just pissing me offIm sure its so simple, but everything I have tried/found doesnt workAll I need is a simple pattern to validate names on my website. Names can have letters (case doesnt matter, must start in a letter), numbers, underscores, and dashesAllowed names:bearrulerbearruler129bear_rulerbear-rulerbe-ar_ru13rIllegal names:_bearrulerbear ruler129bearrulerbear@rulerbearru|er{bearruler}You get the point...Anyway, the php should be[code]$name="The Name They are attempting to use";$pattern='thepattern';if (preg_match($pattern, $name)) echo "ok";//etc...[/code]Does anyone know a pattern that will fit this?Thanks, Bear Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 21, 2006 Share Posted July 21, 2006 Have a read of [url=http://www.phpfreaks.com/forums/index.php/topic,99116.0.html]this topic[/url] maybe. As its basically the same as what you are doing. Quote Link to comment Share on other sites More sharing options...
bearruler Posted July 21, 2006 Author Share Posted July 21, 2006 I tried everything in there, it returns 1 for things like bearruler, but also names like bea$$rrulerBear Quote Link to comment Share on other sites More sharing options...
oracle259 Posted July 21, 2006 Share Posted July 21, 2006 try this pattern:^(?=.*?[\d])?(?=.*?[a-zA-Z])(?=.*?[\-\_])?[a-zA-Z][\-\w]{8,25}$/snote the {8,25} this requires that usernames are at least 8 characters but no more than 25 characters long. Why? I have found that very long usernames cause trouble in the long run. If you want more combinations allow dots(.) or other symbol in your usernames.To remove this restriction simply replace {8,25} with * or + Quote Link to comment Share on other sites More sharing options...
effigy Posted July 21, 2006 Share Posted July 21, 2006 [code]<?php ### Set up test cases. $tests = array( ### Should pass. 'bearruler', 'bearruler129', 'bear_ruler', 'bear-ruler', 'be-ar_ru13r', ### Should fail. '_bearruler', 'bear ruler', '129bearruler', 'bear@ruler', 'bearru|er', '{bearruler}', ); ### Run tests. foreach ($tests as $test) { echo "$test --"; echo preg_match('/^[a-z][-\w]+$/i', $test) ? 'Pass' : 'Fail'; echo '<br />'; }?>[/code] Quote Link to comment 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.