473,624 Members | 2,575 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 4384
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
2400
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. zzz (empty) 4. www g=h,i=j,l=m
4
9740
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 numeric value according to an arbitrary regular expression.
2
3419
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 was splitted. I am fascinated to the powerfull use of this RegEx class, so I wonder if it could go a step further. As a question, can regex be used to valid a set of different functions ? Example : Suppose I have to verify the correctness of an...
2
2666
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: Day, Month, HH, MM, SS, Year, User-Name, Acct-Multi-Session-Id, Acct-Input-Gigawords, Acct-Output-Gigawords, Acct-Input-Octets, Acct-Output-Octets.
8
1851
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 regxUserName = new Regex(@"(?<=User-Name = )\""(+)\""", RegexOptions.None);
3
6027
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 sounds like making sense, but has anyone ever found this useful? Can this 'feature' be disabled? After having used StringTokenizer from the J-language that's not to be named, it's annoyed me for hours before I figured out that it was just a...
5
10104
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 letter of each token (abc) must not be a number. I am simply trying to parse it to get an array of tokens: abc
5
15795
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
1942
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. If it were sed, I could just do s/^"// and s/"$//, but I'm confused with the quote escape characters. Also, can I do both replacements at once? I tried:
0
8179
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8633
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7176
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2613
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 we have to send another system
1
1797
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1493
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.