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 Link to comment https://forums.phpfreaks.com/topic/15274-match-allowed-names/ 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. Link to comment https://forums.phpfreaks.com/topic/15274-match-allowed-names/#findComment-61736 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 Link to comment https://forums.phpfreaks.com/topic/15274-match-allowed-names/#findComment-61759 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 + Link to comment https://forums.phpfreaks.com/topic/15274-match-allowed-names/#findComment-61836 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] Link to comment https://forums.phpfreaks.com/topic/15274-match-allowed-names/#findComment-61837 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.