473,500 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

php regex for nickname input

8 New Member
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
Sep 8 '09 #1
5 4372
Markus
6,050 Recognized Expert Expert
Try this on for size:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     $regex  = '/[^a-zA-Z0-9_-]|anonymous|xxx|yyy/';
  4.     $nicks = array (
  5.         'anonymous',         // Should fail
  6.         'xxy',                // Should pass
  7.         'xxx',                // Should fail
  8.         'yuy',                // Should pass
  9.         'yyy',                // Should fail
  10.         'mark',            // Should pass
  11.         'mark!',            // Should fail
  12.         'mark_skilbeck',            // Should pass
  13.         'mark*skilbeck',       // Should fail
  14.         'mark-skilbeck'        // Should pass
  15.     );
  16.  
  17.     foreach ($nicks as $nick) {
  18.         if (preg_match($regex, $nick)) {
  19.             printf('The nick "%s" did not pass validation.%s',
  20.                     $nick, PHP_EOL);
  21.         } else {
  22.             printf('The nick "%s" passed validation.%s',
  23.                     $nick, PHP_EOL);
  24.         }
  25.     }
  26.  
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.
Sep 8 '09 #2
firstposter
8 New Member
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.
Sep 9 '09 #3
Markus
6,050 Recognized Expert Expert
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.
Sep 9 '09 #4
Dormilich
8,658 Recognized Expert Moderator Expert
note:
Expand|Select|Wrap|Line Numbers
  1. [^a-zA-Z0-9_-] = [^\w-]
  2. [^a-zA-Z0-9_] = \W
Sep 9 '09 #5
firstposter
8 New Member
Ahh cool, thanks guys the code you provided works perfectly
Sep 12 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2369
by: Alan Pretre | last post by:
Can anyone help me figure out a regex pattern for the following input example: xxx:a=b,c=d,yyy:e=f,zzz:www:g=h,i=j,l=m I would want four matches from this: 1. xxx a=b,c=d 2. yyy e=f 3....
4
9700
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
3408
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string...
2
2635
by: rjb | last post by:
Hi there I have a text file which looks like the one below. I'm trying to write a program which will go through each of those lines (for each section separated by an empty line) and give me: ...
8
1838
by: rjb | last post by:
Hi! Could somebody have a look and help me to optimize the code below. It may look like very bad way of coding, but this stuff is very, very new for me. I've included just few lines. Regex...
3
6013
by: Rico | last post by:
If there are consecutive occurrences of characters from the given delimiter, String.Split() and Regex.Split() produce an empty string as the token that's between such consecutive occurrences. It...
5
10084
by: Bob | last post by:
I think this is very simple but I am having difficult doing it. Basically take a comma separated list: abc, def, ghi, jk A list with only one token does not have any commas: abc The first...
5
15786
by: Jiggaz | last post by:
Hi, Look my stored procedure : __________________ ALTER PROCEDURE dbo.CreateAccount @Nickname varchar(30), @Password varchar(15), @Email varchar(50), @Date datetime,
4
1932
by: Flomo Togba Kwele | last post by:
I am having difficulty writing a Regex constructor. A line has a quote(") at its beginning and its end. I need to strip both characters off. If the line looks like "1", I need the result to be 1....
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.