php regex for nickname input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • firstposter
    New Member
    • Aug 2009
    • 8

    php regex for nickname input

    Hello can someone please help improve the regex I have:

    /^[^\s].*[^\s]$/

    It's intended to be used for a nickname field with the following criteria:

    > no spaces

    > exclude the following words(substring ) within the nickname: 'anonymous', 'xxx', 'yyy'

    > just allow letters, numbers, hyphens and underscores eg. exclude characters such as @, #, !, ~, %, ^, *, (, ), =, |, ,(comma) etc


    Thanks
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Try this on for size:

    Code:
    <?php
    
    	$regex  = '/[^a-zA-Z0-9_-]|anonymous|xxx|yyy/';
    	$nicks = array (
    		'anonymous', 		// Should fail
    		'xxy',				// Should pass
    		'xxx',				// Should fail
    		'yuy',				// Should pass
    		'yyy',				// Should fail
    		'mark',			// Should pass
    		'mark!',			// Should fail
    		'mark_skilbeck',	        // Should pass
    		'mark*skilbeck',   	// Should fail
    		'mark-skilbeck'		// Should pass
    	);
    	
    	foreach ($nicks as $nick) {
    		if (preg_match($regex, $nick)) {
    			printf('The nick "%s" did not pass validation.%s',
    					$nick, PHP_EOL);
    		} else {
    			printf('The nick "%s" passed validation.%s',
    					$nick, PHP_EOL);
    		}
    	}
    You might also make the pattern case-insensitive by throwing the 'i' modifier onto the end of the pattern (/[^a-zA-Z0-9_-]|anonymous|xxx| yyy/i).

    Mark.

    Comment

    • firstposter
      New Member
      • Aug 2009
      • 8

      #3
      thanks so much for your post. the code you provided is much better than what i stated with but is it possible to exclude the words WITHIN the nickname.

      i'm wanting to have a list of words that people can't use WITHIN a nickname. words like 'anonymous', 'admin', 'xxx', 'yyy'

      eg. if your website is called 'BlueSky.com' you wouldn't want someone to register a nickname like 'BlueSkyAdmin' or 'BlueSkyStaff'.


      Obviously i'd have to be careful as to which words would be excluded as they might make up legitimate nicknames. eg if 'now' was an excluded word that means that 'snowman' could not be registered.


      exclude words: 'anonymous', 'admin', 'bluesky' 'xxx', 'yyy',
      nicknames that should fail:
      'admin'
      '_admin'
      'admin_user'
      'BlueSky'
      'BlueSkyAdmin'
      'mrbluesky'
      'mr-xxx'
      'mrxxx'
      'xxxyyy'
      'mr_XxxYyy'

      does anyone this this is overkill? i just don't want a malicious user registering a nickname pretending to be an authoritative type member.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Um, if you add your restricted nicknames to the pattern (append them after a | pipe), then the pattern should match them. That is to say, 'test' will be matched in 'tester'.

        Mark.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          note:
          Code:
          [^a-zA-Z0-9_-] = [^\w-]
          [^a-zA-Z0-9_] = \W

          Comment

          • firstposter
            New Member
            • Aug 2009
            • 8

            #6
            Ahh cool, thanks guys the code you provided works perfectly

            Comment

            Working...